python for 循环两个列表同时循环多个列表

for i, j in zip(range(1, 12), range(1, 3)):
    print(i, j)

短的列表循环结束后,整个循环停止
python for 循环两个列表同时循环多个列表_第1张图片

同时循环多个列表
for i, j , k in zip(range(1, 12), range(1, 3), range(1, 4)):
    print(i, j, k)

python for 循环两个列表同时循环多个列表_第2张图片

for f, b in zip(foo, bar):
    print(f, b)

In Python 3, zip returns an iterator of tuples, like itertools.izip in Python2. To get a list of tuples, use list(zip(foo, bar)). And to zip until both iterators are exhausted, you would use itertools.zip_longest.

参考
https://stackoverflow.com/questions/1663807/how-do-i-iterate-through-two-lists-in-parallel

你可能感兴趣的:(python,笔记,python,开发语言)