有一个生成N元组的迭代器,但在这里我会假装我有3.它们的长度都很均匀.
t1 = (a1, b1, c1)
t2 = (a2, b2, c2)
t3 = (a3, b3, c3)
想将上面内容转换为:
d = {a1: [a2, a3],
b1: [b2, b3],
c1: [c2, c3]}
假设列表中的第一个元素不同,使用列表推导来生成结果dict.
lists = [['a1', 'b1', 'c1'], ['a2', 'b2', 'c2'], ['a3', 'b3', 'c3']]
d = {list[0]:list[1:] for list in zip(*lists)}
输出d:
{‘a1’: (‘a2’, ‘a3’), ‘b1’: (‘b2’, ‘b3’), ‘c1’: (‘c2’, ‘c3’)}