简体   繁体   English

在python中迭代两个不同大小的列表

[英]Iterate over two list of different sizes in python

Value = [1,2,3,4,5,6]
content = ['a','b','c','d']

for a,b in itertools.zip_longest(Value , content):
   print(a,b)

The Output that i get using the above code is as follows: 我使用上面的代码得到的输出如下:

1 a
2 b
3 c
4 d
5 None
6 None

what I am Looking for is : 我在寻找的是:

1 a
2 b
3 c
4 d
5 a
6 b

basically once one list is exhausted it should take the values again from starting. 基本上一旦一个列表用尽,它应该从启动时再次取值。 if any one could help would mean alot 如果任何人可以帮助意味着很多

You can use itertools.cycle with zip instead: 您可以使用带有zip itertools.cycle

import itertools
Value = [1,2,3,4,5,6]
content = ['a','b','c','d']

for a,b in zip(Value , itertools.cycle(content)):
   print(a,b)

This outputs: 这输出:

1 a
2 b
3 c
4 d
5 a
6 b

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM