我有两本很长的词典。我想在第二个列表中找到在第一个词典列表中有键的词典,并根据另一个键将它们分开。列表1中的一些键是列表2中的值
举个例子:
students = [{'123': [{'course1': 2}, {'course2': 2}]},
{'124': [{'course1': 3}, {'course2': 4}]},
{'125': [{'course1': 24}, {'course2': 12}]},
{'126': [{'course1': 2}, {'course2': 24}]}, ...]
finals = [{'student_number':'123', 'exam':'passed',...},
{'student_number':'124', 'exam':'ungraded',...},
{'student_number':'125', 'exam':'failed',...}, ...]
# Students who passed exam, 'exam' = 'passed'
passed_students = ['123', ...]
# Other Students
other_students = ['124', '125', ...]
我不太确定您的数据是否为最佳格式,但鉴于您所拥有的,以下代码将起作用:
清单理解的小练习:
结果:
似乎可以通过使用以学生ID为键的字典来简化数据结构:
相关问题 更多 >
编程相关推荐