liz*_*zie 12 python dictionary
它可能是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>}]
Run Code Online (Sandbox Code Playgroud)
我希望得到一个以[0,3,6,9]为键的新词典,并将"myech.MatchingResponse"列表作为值.
当然,我可以使用一个简单的循环来做到这一点,但我想知道是否有更有效的解决方案.
Ott*_*ger 18
import collections
result = collections.defaultdict(list)
for d in dictionaries:
for k, v in d.items():
result[k].append(v)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4222 次 |
| 最近记录: |