现有list: list1 = [{a: 123}, {a: 456},{b: 789}] 合并成: list2 = [{a: [123,456]},{b: [789]}]
from collections import defaultdict lst = [{'a': 123}, {'a': 456},{'b': 789}] dic = {} for _ in lst: for k, v in _.items(): dic.setdefault(k, []).append(v) print [{k:v} for k, v in dic.items()]