Answers:
在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与None。这是的鲜为人知的功能map(但map在Python 3.x中有所更改,因此仅在Python 2.x中有效)。
>>> map(None, a, b, c)
[('a1', 'b1', 'c1'), (None, 'b2', 'c2'), (None, 'b3', None)]
itertools无论如何是内置的C模块。
对于Python 2.6x,请使用itertools模块的izip_longest。
对于Python 3,请zip_longest改用(不加i)。
>>> list(itertools.izip_longest(a, b, c))
[('a1', 'b1', 'c1'), (None, 'b2', 'c2'), (None, 'b3', None)]
six.moves.zip_longest改用。
我使用2d数组,但是使用python 2.x的概念相似:
if len(set([len(p) for p in printer])) > 1:
printer = [column+['']*(max([len(p) for p in printer])-len(column)) for column in printer]