【发布时间】:2019-08-05 23:09:35
【问题描述】:
我没有找到我要找的答案,所以我自己想出来了,想和你分享。这些:Python equivalent of zip for dictionaries 不实现“默认值”(又名最长风味),也不保持顺序。
如何以“zip_longest”风格在 python 中压缩OrderedDicts?
拥有:
from collections import OrderedDict
o1 = OrderedDict([("key_a", "a1"), ("key_b", "b1")])
o2 = OrderedDict([("key_a", "a2"), ("key_h", "h2")])
o3 = OrderedDict([("key_c", "c3")])
o4 = OrderedDict([("key_x", "x4")])
如何从传递的每个 OrderedDict 中获取成对的键和对应的压缩值,但如果缺少值,则使用 None?
expected_result = [
('key_a', ('a1', 'a2', None, None)),
('key_b', ('b1', None, None, None)),
('key_h', (None, 'h2', None, None)),
('key_c', (None, None, 'c3', None)),
('key_x', (None, None, None, 'x4')),
]
【问题讨论】:
标签: python python-3.x python-2.7 dictionary