将多个zip对象串联到一个列表中

时间:2019-04-12 15:12:02

标签: python

我使用列表推导来生成许多zip对象:

[ zip(a, b[i]) for i in range(0, 1) ]

>>> [<zip object at 0x10a216b88>, <zip object at 0x10a216c08>]

如何将两个zip对象连接到一个列表中?

例如,<zip object at 0x10a216b88>具有:

(a, b)
(a, c)

<zip object at 0x10a216c08>

(f, g)
(f, w)

所需的输出将是:

[(a, b), (a, c), (f, g), (f, w)]

1 个答案:

答案 0 :(得分:0)

使用itertools.chain.from_iterable

>>> import itertools
>>> l = [zip(range(3), range(3)), zip(range(3), range(3))]
>>> l
[<zip object at 0x7f7e80912408>, <zip object at 0x7f7e840a18c8>]
>>> list(itertools.chain.from_iterable(l))
[(0, 0), (1, 1), (2, 2), (0, 0), (1, 1), (2, 2)]