[英]How do I extend all elements of a list to another list every Nth item
我正在嘗試找到一種方法,從列表中的每個第 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
函數將允許用戶以“nth”間隔將元素放入單獨的列表中。 在這個例子中,我將 l1 的列表放在 l2 列表中每第二個元素之后(因為 len(l1) 等於 2)。 collapse
函數將采用放置在 l2 中的列表,並將每個元素按順序排列為一個單獨的元素。
感謝所有幫助過我的人。 我喜歡學習新東西。
numpy 能夠將列表拆分為均勻分布的較小列表。 您可以在np.array_split
中指定列表本身,以及拆分的數量。 在這里,我使用 math.floor 來獲取n
值進入列表長度的偶數次。 在這種情況下,您有n=3
並且列表有 12 個元素,因此它將返回 4 作為結果子列表的數量。
[np.append(x,l1) for x....
部分表示將 l1 中的值附加到每個子列表的末尾。 並且chain_from_iterable
會將它們全部混合在一起,您可以將其呈現為list()
。
它也有在最后添加l1
值的副作用,如果您不想這樣做,可以使用切片刪除最后n
值,其中n
是l1
列表的長度。
import numpy as np
import itertools
import math
n = 3
l1 = ["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)]
只需遍歷列表 b 的每個第 n 個元素並插入列表 a 的每次迭代。
a = ["a", "b"]
b = ["j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u"]
n = 3 # where you want to insert
while n < len(b):
for i in range(len(a)):
b.insert(n, a[i])
n += 1 # increment n by 1 so 'b' insert after 'a'
n += 3 # increment n by 3 every iteration to insert list a
print(b)
結果:['j', 'k', 'l', 'a', 'b', 'm', 'n', 'o', 'a', 'b', 'p', 'q' , 'r', 'a', 'b', 's', 't', 'u']
list3 = []
n = 3
for 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)
list1= ["a","b"]
list= ["j","k","l","m","n","o","p","q","r","s","t","u"]
d=[]
for i in list:
print(list.index(i))
if ((list.index(i)+1)%3==0 and list.index(i)!=0):
d.append(i)
d.extend(list1)
else:
d.append(i)
print(d)
one = ["a","b"]
two = ["j","k","l","m","n","o","p","q","r","s","t","u"]
start =0
end = 3 #or whatever number you require
complete_list=[]
iterations = int(len(two)/3)
for x in range(iterations):
sub_list= two[start:end]
start = start+3
end= end+3
complete_list.append(sub_list)
if x < iterations-1:
complete_list.append(one)
complete_list = flatten(complete_list)
可能有更短的代碼版本可以做到這一點,但這也能正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.