在Python 3中你可以使用itertools.zip_longest
>>> list(itertools.zip_longest(a, b, c))
[('a1', 'b1', 'c1'), (None, 'b2', 'c2'), (None, 'b3', None)]
您可以使用与以下不同的值填充None
通过使用fillvalue
范围:
>>> list(itertools.zip_longest(a, b, c, fillvalue='foo'))
[('a1', 'b1', 'c1'), ('foo', 'b2', 'c2'), ('foo', 'b3', 'foo')]
对于 Python 2,您可以使用itertools.izip_longest(Python 2.6+),或者你可以使用map
with None
。这是一个鲜为人知的的特征map (but map
在 Python 3.x 中发生了变化,因此这只适用于 Python 2.x)。
>>> map(None, a, b, c)
[('a1', 'b1', 'c1'), (None, 'b2', 'c2'), (None, 'b3', None)]