是否有一个内置函数,工作类似zip(),但将填充结果,使结果列表的长度是最长的输入而不是最短的输入的长度?
>>> a = ['a1']
>>> b = ['b1', 'b2', 'b3']
>>> c = ['c1', 'c2']
>>> zip(a, b, c)
[('a1', 'b1', 'c1')]
>>> What command goes here?
[('a1', 'b1', 'c1'), (None, 'b2', 'c2'), (None, 'b3', None)]
是否有一个内置函数,工作类似zip(),但将填充结果,使结果列表的长度是最长的输入而不是最短的输入的长度?
>>> a = ['a1']
>>> b = ['b1', 'b2', 'b3']
>>> c = ['c1', 'c2']
>>> zip(a, b, c)
[('a1', 'b1', 'c1')]
>>> What command goes here?
[('a1', 'b1', 'c1'), (None, 'b2', 'c2'), (None, 'b3', None)]
当前回答
除了接受的答案外,如果你使用的可迭代对象可能长度不同,但不应该是,建议将strict=True传递给zip()(自Python 3.10起支持)。
引用文件:
zip() is often used in cases where the iterables are assumed to be of equal length. In such cases, it’s recommended to use the strict=True option. Its output is the same as regular zip(): >>> list(zip(('a', 'b', 'c'), (1, 2, 3), strict=True)) [('a', 1), ('b', 2), ('c', 3)] Unlike the default behavior, it checks that the lengths of iterables are identical, raising a ValueError if they aren’t: >>> list(zip(range(3), ['fee', 'fi', 'fo', 'fum'], strict=True)) Traceback (most recent call last): ... ValueError: zip() argument 2 is longer than argument 1 Without the strict=True argument, any bug that results in iterables of different lengths will be silenced, possibly manifesting as a hard-to-find bug in another part of the program.
其他回答
non itertools Python 3 解决方案:
def zip_longest(*lists):
def g(l):
for item in l:
yield item
while True:
yield None
gens = [g(l) for l in lists]
for _ in range(max(map(len, lists))):
yield tuple(next(g) for g in gens)
我的Python 2解决方案:
if len(list1) < len(list2):
list1.extend([None] * (len(list2) - len(list1)))
else:
list2.extend([None] * (len(list1) - len(list2)))
除了接受的答案外,如果你使用的可迭代对象可能长度不同,但不应该是,建议将strict=True传递给zip()(自Python 3.10起支持)。
引用文件:
zip() is often used in cases where the iterables are assumed to be of equal length. In such cases, it’s recommended to use the strict=True option. Its output is the same as regular zip(): >>> list(zip(('a', 'b', 'c'), (1, 2, 3), strict=True)) [('a', 1), ('b', 2), ('c', 3)] Unlike the default behavior, it checks that the lengths of iterables are identical, raising a ValueError if they aren’t: >>> list(zip(range(3), ['fee', 'fi', 'fo', 'fum'], strict=True)) Traceback (most recent call last): ... ValueError: zip() argument 2 is longer than argument 1 Without the strict=True argument, any bug that results in iterables of different lengths will be silenced, possibly manifesting as a hard-to-find bug in another part of the program.
在Python 3中,您可以使用itertools.zip_longest
>>> list(itertools.zip_longest(a, b, c))
[('a1', 'b1', 'c1'), (None, 'b2', 'c2'), (None, 'b3', None)]
你可以使用fillvalue参数填充不同于None的值:
>>> 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)]
只是使用迭代器,没什么特别的。
def zip_longest(*iterables):
items = 0
for iterable in iterables:
items = max(items, len(iterable))
iters = [iter(iterable) for iterable in iterables]
while items:
yield (*[next(i, None) for i in iters],)
items -= 1