成语大全网 - 汉语词典 - python组合词典

python组合词典

info=?[{'name':'apple','value':2},{'name':'orange','value':5},{'name':'apple','value':5}]

info_dic={}

for?d?in?info:

if?d['name']?not?in?info_dic:

info_dic[d['name']]=d['value']

else:?info_dic[d['name']]+=d['value']

new_info=[]

for?n,v?in?info_dic.items():

new_info.append({'name':n,'value':v})

print(new_info)[{'name':?'apple',?'value':?7},?{'name':?'orange',?'value':?5}]