如何将列表中的所有元素每第 N 个项目扩展到另一个列表

我正在尝试找到一种方法,将列表中的所有元素插入另一个列表的每个第 n 个元素。有几个类似的帖子,但它们是针对一个列表的单个元素到另一个列表的。我想弄清楚的是采取以下列表:


l1 = ["a","b"]

l2 = ["j","k","l","m","n","o","p","q","r","s","t"u"]

并将它们一起输出:


["j","k","l","a","b","m","n","o","a","b","p","q","r","a","b","s","t","u"]

我认为行之有效的方法至少是这样开始的:


for x in 1st:

    for i in range(len(l2)):

        2nd.extend(l1)

我知道这行不通,但我不知道如何实施。


在上面的特定输出中,第一个列表添加到每第三个元素之后。它不必是每三个元素,但我只是用它作为例子。


有人可以教我如何做到这一点吗?


编辑: 在@Chris的帮助下,我找到了一个名为的第三方库more_itertools,并创建了一个新代码,它完全符合我的要求。这是我想出的:


import more_itertools as mit


l1 = ["a","b"]

l2 = ["j","k","l","m","n","o","p","q","r","s","t","u"]


#We will place the elements of 1st after every two elements of 2nd 

l3 = list(mit.collapse(mit.intersperse(l1, l2, n=len(l1))))

结果:


>>> print(l3)

['j', 'k', 'a', 'b', 'l', 'm', 'a', 'b', 'n', 'o', 'a', 'b', 'p', 'q', 'a', 

'b', 'r', 's', 'a', 'b', 't', 'u']

我发现该intersperse函数将允许用户将元素以“第 n”间隔放置到单独的列表中。在此示例中,我将 l1 列表放置在 l2 列表中的每隔一个元素之后(因为 len(l1) 等于 2)。该collapse函数将获取放置在 l2 中的列表,并将每个元素作为单独的元素按顺序排列。


感谢所有在这方面帮助过我的人。我喜欢学习新东西。


Helenr
浏览 124回答 5
5回答

DIEA

numpy 能够将列表拆分为均匀分布的较小列表。您可以在np.array_split列表本身中指定拆分数量。n在这里,我使用 math.floor 来获取该值进入列表长度的偶数次。在本例中,n=3列表有 12 个元素,因此它将返回 4 作为结果子列表的数量。该[np.append(x,l1) for x....部分表示将 l1 中的值附加到每个子列表的末尾。并将chain_from_iterable它们全部混合在一起,您可以将其呈现为列表list()。l1它还有在最后添加值的副作用,如果您不想这样做,可以使用切片来删除最后一个n值,其中n是列表的长度l1。import numpy as npimport itertoolsimport mathn = 3l1 = ["a","b"]l2 = ["j","k","l","m","n","o","p","q","r","s","t","u"]list(itertools.chain.from_iterable([np.append(x, l1) for x in np.array_split(l2,math.floor(len(l2)) / n)]))或者如果您不想尾随附加:list(itertools.chain.from_iterable([np.append(x,                                               l1) for x in np.array_split(l2,                                                                          math.floor(len(l2)) / n)]))[:-len(l1)]

白猪掌柜的

list3 = []n = 3for x in range(1, len(list2)+1):    if x%n == 0 and x != len(list2):        list3.append(list2[x-1])        for y in list1:            list3.append(y)    else:        list3.append(list2[x-1])print(list3)

泛舟湖上清波郎朗

只需迭代列表 b 的每个第 n 个元素并插入列表 a 的每个迭代即可。&nbsp; &nbsp; a = ["a", "b"]&nbsp; &nbsp; b = ["j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u"]&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; n = 3&nbsp; # where you want to insert&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; while n < len(b):&nbsp; &nbsp; &nbsp; &nbsp; for i in range(len(a)):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b.insert(n, a[i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; n += 1&nbsp; # increment n by 1 so 'b' insert after 'a'&nbsp; &nbsp; &nbsp; &nbsp; n += 3&nbsp; # increment n by 3 every iteration to insert list a&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; print(b)结果:['j', 'k', 'l', 'a', 'b', 'm', 'n', 'o', 'a', 'b', 'p', 'q' , 'r', 'a', 'b', 's', 't', 'u']

侃侃无极

list1= ["a","b"]list= ["j","k","l","m","n","o","p","q","r","s","t","u"]d=[]for i in list:&nbsp; &nbsp; print(list.index(i))&nbsp; &nbsp; if ((list.index(i)+1)%3==0 and list.index(i)!=0):&nbsp; &nbsp; &nbsp; &nbsp; d.append(i)&nbsp; &nbsp; &nbsp; &nbsp; d.extend(list1)&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; d.append(i)print(d)

手掌心

one = ["a","b"]two = ["j","k","l","m","n","o","p","q","r","s","t","u"]start =0end = 3 #or whatever number you requirecomplete_list=[]iterations = int(len(two)/3)for x in range(iterations):&nbsp; &nbsp; sub_list= two[start:end]&nbsp; &nbsp;&nbsp; &nbsp; start = start+3&nbsp; &nbsp; end= end+3&nbsp; &nbsp; complete_list.append(sub_list)&nbsp; &nbsp; if x < iterations-1:&nbsp; &nbsp; &nbsp; &nbsp; complete_list.append(one)complete_list = flatten(complete_list)可能有更短版本的代码可以执行此操作,但这也可以工作。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python