TypeError: string indices must be integers, not str // working with dict

前端 未结 3 477
醉酒成梦
醉酒成梦 2020-12-14 05:38

I am trying to define a procedure, involved(courses, person), that takes as input a courses structure and a person and returns a Dictionary that describes all t

3条回答
  •  粉色の甜心
    2020-12-14 06:34

    I see that you are looking for an implementation of the problem more than solving that error. Here you have a possible solution:

    from itertools import chain
    
    def involved(courses, person):
        courses_info = chain.from_iterable(x.values() for x in courses.values())
        return filter(lambda x: x['teacher'] == person, courses_info)
    
    print involved(courses, 'Dave')
    

    The first thing I do is getting the list of the courses and then filter by teacher's name.

提交回复
热议问题