如何压缩长度不均匀的列表

Soh*_*amC 0 python list-comprehension python-2.7

我有 3 个列表。

>>> a = [1,2,3,4,5]
>>> b = ['a','b','c','d']
>>> c = ['x','y','z'] 
Run Code Online (Sandbox Code Playgroud)

我希望以不遗漏任何元素的方式压缩它们。python中是否存在这样的函数可以执行以下任务?

>>> myzip(a,b,c)
[(1, 'a','x'), (2, 'b','y'), (3, 'c','z'), (4,'d'), (5)]
Run Code Online (Sandbox Code Playgroud)

提前致谢!!

the*_*eye 5

您可以itertools.izip_longest像这样使用和列出理解

from itertools import izip_longest
print [tuple(j for j in i if j is not None)for i in izip_longest(a, b, c)]
# [(1, 'a', 'x'), (2, 'b', 'y'), (3, 'c', 'z'), (4, 'd'), (5,)]
Run Code Online (Sandbox Code Playgroud)