python大列表分割成小列表,在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:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值