简体   繁体   English

遍历列表并依次 append 每个元素到一组新列表

[英]Iterate over list and sequentially append each element to a set number of new lists

I'm trying to iterate over a list to split it into new lists, with the elements sequentially assigned as shown below, with the first element appended to List1 , the second appended to List2 , and so on, then starting again at List1 for the fifth element, List2 for the sixth element, and so on.我正在尝试迭代一个列表以将其拆分为新列表,按顺序分配元素,如下所示,第一个元素附加到List1 ,第二个附加到List2 ,依此类推,然后再次从List1开始第五个元素, List2为第六个元素,依此类推。

Existing list:现有名单:

main_list = ['apples', 'oranges', 'bananas', 'mangoes', 'kiwis', 'grapes', 'watermelon', 'tangerines', 'plums', 'dragonfruit']

Here's what I want:这就是我想要的:

list1 = ['apples', 'kiwis', 'plums']
list2 = ['oranges', 'grapes', 'dragonfruit']
list3 = ['bananas', 'watermelon']
list4 = ['mangoes', 'tangerines']

I'm sure has been asked elsewhere, but I can't seem to track it down.我肯定在其他地方被问过,但我似乎无法找到它。

Slice the list to get the desired results for each target:对列表进行切片以获得每个目标的所需结果:

list1 = main_list[::4]
list2 = main_list[1::4]
list3 = main_list[2::4]
list4 = main_list[3::4]

For something a little more dynamic and supports an n-number of lists, you can use itertools.cycle对于更动态并支持 n 个列表的东西,您可以使用itertools.cycle

main_list = ['apples', 'oranges', 'bananas', 'mangoes', 'kiwis', 'grapes', 'watermelon', 'tangerines', 'plums',
             'dragonfruit']

number_of_lists = 4
lists = [[] for _ in range(number_of_lists)]
cycler = cycle(lists)

for ml in main_list:
    next(cycler).append(ml)

for l in lists:
    print(l)


>>> ['apples', 'kiwis', 'plums']
>>> ['oranges', 'grapes', 'dragonfruit']
>>> ['bananas', 'watermelon']
>>> ['mangoes', 'tangerines']

暂无
暂无

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

相关问题 迭代两个相等长度的列表并将每对最大值添加到新列表的更简单/首选方法? - Simpler/preferred way to iterate over two equal-length lists and append the max of each pair to a new list? 多次遍历三个列表,在每次迭代中从列表中依次选择一个元素 - Iterate through three lists multiple times sequentially picking one element from a list in each iteration 迭代列表列表并推导出最终列表,以便可以对 Python 中最终列表的每个元素进行分组 - Iterate over list of lists and deduce a final list so that group by can be done on each element of final list in python 如何迭代三个或更多列表,将每个列表与每个元素进行比较? - How to iterate over three or more lists, comparing each with each element? 如何在将列表中的每个值传递到 API 并在每个列表列表后暂停时迭代列表列表? - How to iterate over list of lists while passing each value in the lists into API and pausing after each list of list? Pandas 列表列,append 每个列表的新列 - Pandas column of lists, append a new column to each list 如何迭代重复Python中每个元素的列表 - How to iterate over a list repeating each element in Python 遍历字符串并将模式移动到每个列表元素的开头 - iterate over strings and move pattern to start of each list element 如何迭代并将列表中的每个元素与另一个元素相加以找到一个数字? - How to iterate and sum each element in a list with another to find a number? 如何遍历 2 个巨大的列表并查找列表 1 中的每个元素是否是列表 2 元素的一部分? - How to iterate through 2 huge lists and find if each element in list 1 is a part of an element of list 2?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM