循环遍历2个列表,重复最短直到最长的结尾 [英] Loop over 2 lists, repeating the shortest until end of longest
本文介绍了循环遍历2个列表,重复最短直到最长的结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我敢肯定有一个简单而明显的方法可以做到这一点,但是我一直在谷歌搜索并阅读文档,但是我什么都找不到.
I am sure there is an easy and obvious way to do this, but I have been googling and reading the docs and I just cannot find anything.
这是我要实现的目标:
la = ['a1', 'a2', 'a3', 'a4']
lb = ['b1', 'b2']
result = ['a1_b1', 'a2_b2', 'a3_b1', 'a4_b2']
我有一个日期列表,其中一些带有标记.然后,我有一个更大的日期列表,我想将最小的列表尽可能多地放入更大的列表中.可能需要某种循环,因为我需要访问较大列表中的日期才能获得最终结果.
I have a list of dates and some of them have something marked on them. I then have a much larger list of dates and I want to put the smallest list inside the bigger list as many times as possible. It will probably require some sort of loop as I need access to the dates in the larger list for the end result.
由于某种原因,我只是看不到一个好方法.
For some reason, I just cannot see a good way to do this.
推荐答案
尝试
result = ["_".join((i, j)) for i, j in itertools.izip(la, itertools.cycle(lb))]
这篇关于循环遍历2个列表,重复最短直到最长的结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文