Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 单个for循环中的多次迭代_Python_Python 3.x - Fatal编程技术网

Python 单个for循环中的多次迭代

Python 单个for循环中的多次迭代,python,python-3.x,Python,Python 3.x,我有 list1 = [1, 2, 3, 4, 5] list2 = [7, 8, 9, 14, 25, 36] list3 = [43, 65] 我想要实现的是 1 7 43 2 8 65 3 9 4 14 5 25 36 我试过寻找像itertools和multi-processing这样的方法,但没有一种有用 有没有一种方法可以在一个for循环中实现这一点,比如 for I, J, K inb list1, list2, list3: print('{} {} {}'.fo

我有

list1 = [1, 2, 3, 4, 5]
list2 = [7, 8, 9, 14, 25, 36]
list3 = [43, 65]
我想要实现的是

1 7 43
2 8 65
3 9
4 14
5 25
  36
我试过寻找像
itertools
multi-processing
这样的方法,但没有一种有用

有没有一种方法可以在一个for循环中实现这一点,比如

for I, J, K inb list1, list2, list3:
    print('{} {} {}'.format(I, J, K))
有什么帮助吗

编辑

zip函数从列表中获取最少数量的元素,如果使用zip,输出将是

1 7 43
2 8 65

您可以使用itertools尝试此操作:

import itertools

list1 = [1, 2, 3, 4, 5]
list2 = [7, 8, 9, 14, 25, 36]
list3 = [43, 65]

for a, b, c in itertools.izip_longest(list1, list2, list3):
     print a, b, c

哦,看,上面的答案使用了
itertools
中的函数!您需要zip longest函数,但当其中一个函数在
itertools中被检查时,它将返回none。izip_longest
请阅读链接页面。您可以将
fillvalue
设置为其他值。