循环遍历 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屋!
查看全文