Python: 寻找在列表中的字典,其包含其他列表中的一些字典的键

2024-09-26 18:06:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两本很长的词典。我想在第二个列表中找到在第一个词典列表中有键的词典,并根据另一个键将它们分开。列表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', ...]

Tags: number列表student学生例子词典examfailed
3条回答
>>> students = {'123':{'name':'Bonnie','course_1':2, 'course_2':2},
...             '124':{'name':'Jerry', 'course_1':3, 'course_2':4},
...             '125':{'name':'Bob', 'course_1':24, 'course_2':12},
...             '126':{'name':'Jill', 'course_1':2, 'course_2':24}}
>>> finals = [{'num':'123', 'exam':'passed'},
...           {'num':'124', 'exam':'ungraded'},
...           {'num':'125', 'exam':'failed'}]
>>> student_results = {'passed':[], 'ungraded':[], 'failed':[]}
>>>
>>> for final in finals:
...    student_results[final['exam']].append(students[final['num']])
>>>        
>>> # Print student results.
>>> for result in ['passed', 'ungraded', 'failed']:
...     print "Students %s:" % result
...     for student in student_results[result]:
...         print "   " + student['name']
...
Students passed:
   Bonnie
Students ungraded:
   Jerry
Students failed:
   Bob        

我不太确定您的数据是否为最佳格式,但鉴于您所拥有的,以下代码将起作用:

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'}]

studentIDs = [i.keys()[0] for i in students]

passed_students=[]
other_students=[]
for row in finals:
    snum = row['student_number']
    status = row['exam']
    if status=='passed' and snum in studentIDs:
        passed_students.append(snum)
    elif status!='passed' and snum in studentIDs:
        other_students.append(snum)
    else:
        print 'Student ID {0} not found in list.'.format(snum)

清单理解的小练习:

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',},]

# Extract student id numbers.
student_ids = set(
    student_data.keys()[0]
    for student_data in students)

# Restrict finals to the students that exist in students.
students_with_finals = [
    final
    for final in finals
    if final['student_number'] in student_ids]

passed_students = [
    final['student_number']
    for final in students_with_finals
    if final['exam'] == 'passed']

other_students = [
    final['student_number']
    for final in students_with_finals
    if final['exam'] != 'passed']

print('Passed students: {}'.format(passed_students))
print('Other students: {}'.format(other_students))

结果:

Passed students: ['123']
Other students: ['124', '125']

似乎可以通过使用以学生ID为键的字典来简化数据结构:

students = {
    '123': [{'course1': 2}, {'course2': 2}],
    '124': [{'course1': 3}, {'course2': 4}],
    '125': [{'course1': 24}, {'course2': 12}],
    '126': [{'course1': 2}, {'course2': 24}],
}

finals = {
    '123': {'exam':'passed', 'points': 100},
    '124': {'exam':'ungraded'},
    '125': {'exam':'failed'},
}

相关问题 更多 >

    热门问题