鉴于字典:
dictData = [{'aaa': 'aaa_value1', 'bbb': 'bbb_value1', 'ccc': 'ccc_value1'},
{'aaa': 'aaa_value1', 'bbb': 'bbb_value2', 'ccc': 'ccc_value2'},
{'aaa': 'aaa_value2', 'bbb': 'bbb_value3', 'ccc': 'ccc_value3'}]
我想要这个:
newdictData=[{'aaa_value1':[(bbb_value1,ccc_value1),(bbb_value2, ccc_value2)],
{'aaa_value2':[(bbb_value3,ccc_value3)],
答案 0 :(得分:0)
您可以使用defaultdict:
from collections import defaultdict
d = defaultdict(list)
dictData = [{'aaa': 'aaa_value1', 'bbb': 'bbb_value1', 'ccc': 'ccc_value1'},
{'aaa': 'aaa_value1', 'bbb': 'bbb_value2', 'ccc': 'ccc_value2'},
{'aaa': 'aaa_value2', 'bbb': 'bbb_value3', 'ccc': 'ccc_value3'}]
for i in dictData:
the_vals = sorted(i.values())
d[the_vals[0]].append(the_vals[1:])
print(dict(d))
输出:
{'aaa_value1': [['bbb_value1', 'ccc_value1'], ['bbb_value2', 'ccc_value2']], 'aaa_value2': [['bbb_value3', 'ccc_value3']]}