python当中如果有一个列表里面是多个字典,且有一对键值对相同,想要求不同的键对应的值之和
a = [{"id":1,"count":29},{"id':1,"count":39}] ids = list(set([u["id"] for u in a])) # [1,] b = [] for id in ids: count = 0 for u in a: if id in u.get('id'): count+= count b.append({"id':id,"count":count}) print(b) # [{"id":1,"count":68}]
方式二:
python result = {} for dict1 in list1: key = dict1.get('key1') if key in result: result[key] += dict1[key] else: result[key] = dict1[key]