这可能是Python中的经典问题,但是我还没有找到答案。
我有一个词典列表,这些词典具有相似的键。
看起来像这样:
[{0: myech.MatchingResponse at 0x10d6f7fd0,
3: myech.MatchingResponse at 0x10d9886d0,
6: myech.MatchingResponse at 0x10d6f7d90,
9: myech.MatchingResponse at 0x10d988ad0},
{0: myech.MatchingResponse at 0x10d6f7b10,
3: myech.MatchingResponse at 0x10d6f7f90>}]
我想以[0,3,6,9]作为键,并以“myech.MatchingResponse”列表作为值来获取一个新字典。
当然,我可以使用一个简单的循环来执行此操作,但我想知道是否有更有效的解决方案。
最佳答案
import collections
result = collections.defaultdict(list)
for d in dictionaries:
for k, v in d.items():
result[k].append(v)
关于python - 如何在Python中将字典列表转换为列表字典?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11450575/