按照你的要求编写的字典内相同key合并的Python程序如下
l=[{'a':1,'b':'haha'},{'a':3,'b':'haha'},{'a':2,'b':'xiaoming'}]
result=[]
temp=[]
for i in range(len(l)):
flag=False
suma=l[i]['a']
b=l[i]['b']
for j in range(i+1,len(l)):
if l[i]['b']==l[j]['b'] and (j not in temp):
flag=True
temp.append(i)
temp.append(j)
suma=suma+l[j]['a']
if i not in temp or flag==True:
result.append({'a':suma,'b':b})
print(result)
源代码(注意源代码的缩进)