我有一本字典如下:
P_ID V1 V2 V3
100 slow slow high
100 slow high high
104 medium high high
107 slow high high
107 slow high high
107 slow high high
...
...
所以我想根据P_ID的值将这个字典拆分成多个字典。像这样:
Dic 1:
P_ID V1 V2 V3
100 slow slow high
100 slow high high
Dic 2:
P_ID V1 V2 V3
104 medium high high
Dic 3:
P_ID V1 V2 V3
107 slow high high
107 slow high high
107 slow high high
答案 0 :(得分:0)
试试这个。可能还有其他方法,但我认为这个方法更简单。
d = [{'V1': 'slow', 'V2': 'slow', 'V3': 'high', 'P_ID': 100},
{'V1': 'slow', 'V2': 'high', 'V3': 'high', 'P_ID': 100},
{'V1': 'medium', 'V2': 'high', 'V3': 'high', 'P_ID': 104},
{'V1': 'slow', 'V2': 'high', 'V3': 'high', 'P_ID': 107},
{'V1': 'slow', 'V2': 'high', 'V3': 'high', 'P_ID': 107},
{'V1': 'slow', 'V2': 'high', 'V3': 'high', 'P_ID': 107}]
keys = set([e['P_ID'] for e in d])
for k in keys:
print [x for x in d if x['P_ID'] == k]
print("")
在线演示:https://repl.it/JOn7