當前位置:成語大全網 - 新華字典 - python字典問題?

python字典問題?

dict1?=?{'score':?'12',?'name':?'john'}

dict2?=?{'score':?'10',?'name':?'john'}

dict3?=?{'score':?'24',?'name':?'mike'}

l?=?[dict1,dict2,dict3]

def?group_key(d):

return?d['name']

l?=?sorted(l,key=group_key)

import?itertools

for?key,group?in?itertools.groupby(l,group_key):

i = sum([int(item['score']) for item in group])

print?{'score':str(i),'name':key}