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)
提前致谢!!
您可以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)