在python中合并字典列表中的重复项

在python中合并字典列表中的重复项,python,list,dictionary,Python,List,Dictionary,我有一个python词典列表。现在我正试图根据python中的一个特定键实体合并这些字典。示例字典列表为: [[{'max_score': u'110', 'total_mark': u'75', 'student': <student_details: Komal>}, {'max_score': u'110', 'total_mark': u'75', 'student': <student_details: Java>}], [{'max_score': u'13

我有一个python词典列表。现在我正试图根据python中的一个特定键实体合并这些字典。示例字典列表为:

[[{'max_score': u'110', 'total_mark': u'75', 'student': <student_details: Komal>}, {'max_score': u'110', 'total_mark': u'75', 'student': <student_details: Java>}],
 [{'max_score': u'131', 'total_mark': u'99', 'student': <student_details: Komal>}, {'max_score': u'131', 'total_mark': u'64', 'student': <student_details: Java>}],
 [{'max_score': u'138', 'total_mark': u'110', 'student': <student_details: Komal>}, {'max_score': u'138', 'total_mark': u'80', 'student': <student_details: Java>}]]
[{'max_分数:u'110','total_分数:u'75','student':},{'max_分数:u'110','total_分数:u'75','student':}],
[{'max_分数:u'131','total_分数:u'99','student':},{'max_分数:u'131','total_分数:u'64','student':},
[{'max_分数:u'138','total_分数:u'110','student':},{'max_分数:u'138','total_分数:u'80','student':}]
我正试图将此记录转换为他们的单个记录,如:

    ['student': <student_details: Komal>:[
{'max_score': u'110', 'total_mark': u'75', }
{'max_score': u'131', 'total_mark': u'99'},
{'max_score': u'138', 'total_mark': u'110'}],
'student': <student_details: Java>:[
{'max_score': u'110', 'total_mark': u'75'}, 
{'max_score': u'131', 'total_mark': u'64'}, 
{'max_score': u'138', 'total_mark': u'80'}]]
[“学生”:[
{'max_分数:u'110','total_分数:u'75',}
{'max_score':u'131','total_score':u'99'},
{'max_score':u'138','total_score':u'110'},
“学生”:[
{'max_score':u'110','total_score':u'75'},
{'max_score':u'131','total_score':u'64'},
{'max_分数:u'138','total_分数:u'80'}]]

请告诉我如何才能做到这一点。提前感谢。

最接近预期输出的可能是:

studs = [
 [{'max_score': u'110', 'total_mark': u'75', 'student': '<student_details: Komal>'}, 
  {'max_score': u'110', 'total_mark': u'75', 'student': '<student_details: Java>'}], 
 [{'max_score': u'131', 'total_mark': u'99', 'student': '<student_details: Komal>'}, 
  {'max_score': u'131', 'total_mark': u'64', 'student': '<student_details: Java>'}], 
 [{'max_score': u'138', 'total_mark': u'110', 'student': '<student_details: Komal>'}, 
  {'max_score': u'138', 'total_mark': u'80', 'student': '<student_details: Java>'}]]


d={}
for studlist in studs:
    for stud in studlist:
        # use the 'student' - entry as tuple as key and append a set of each scores data
        d.setdefault( ('student',stud['student']) , []).append(
            { 'max_score' : stud['max_score'], 'total_mark': stud['total_mark'] })

print(d)
螺柱=[
[{'max_分数:u'110','total_分数:u'75','student':'''},
{'max_分数:u'110','total_分数:u'75','student':''},
[{'max_分数:u'131','total_分数:u'99','student':'''},
{'max_score':u'131','total_score':u'64','student':''},
[{'max_score':u'138','total_score':u'110','student':'''},
{'max_分数:u'138','total_分数:u'80','student':''}]
d={}
对于螺柱中的螺柱列表:
对于双头螺栓列表中的双头螺栓:
#使用“学生”条目作为元组作为键,并附加一组每个分数数据
d、 setdefault(('student',student['student']),[])。追加(
{'max_score':螺柱['max_score'],'total_mark':螺柱['total_mark']})
印刷品(d)
输出:

{('student', '<student_details: Komal>'): 
    [{'max_score': '110', 'total_mark': '75'}, 
     {'max_score': '131', 'total_mark': '99'}, 
     {'max_score': '138', 'total_mark': '110'}], 
('student', '<student_details: Java>'): 
    [{'max_score': '110', 'total_mark': '75'}, 
     {'max_score': '131', 'total_mark': '64'}, 
     {'max_score': '138', 'total_mark': '80'}]
} 
{('学生',''):
[{'max_score':'110','total_score':'75'},
{'max_score':'131','total_score':'99'},
{'max_score':'138','total_score':'110'},
(‘学生’,“”):
[{'max_score':'110','total_score':'75'},
{'max_score':'131','total_score':'64'},
{‘最高分数’:‘138’,‘总分’:‘80’}]
} 

这是一个将
元组
设置为
,元组是
(“学生”,“你的详细信息”)
和你分数的
列表
的值。您需要一个可散列类型作为dict的键-元组是不可变的,因此可散列且作为键有效。

我不理解您的输出结构。是列表还是字典?这是一个很好的例子,可以很容易地翻译为使用
集合。defaultdict
。但它能满足OP的要求,是一个很好的解决方案+1@jp_data_analysisOP是在胡说八道。用猜测来赞扬一个答案可能有点过分。它做的事情尽可能接近他问的内容。将某些对象类型转换为字符串是有争议的,但可以澄清。一点也不多。谢谢你的帮助。但是我怎么能在循环中迭代数据呢?正常的迭代是不可行的。也许我不应该回答一个不是100%清晰的问题。。。