简体   繁体   English

将列表中的一个元素插入另一个列表的第 k 个 position 每个子列表(插入 k+=1 后从左到右)- Python

[英]Insert an element in a list into kth position each sublist (from left to right after inserting k+=1) of another list- Python

There are 3 subsets in Spi, each subset contains its sublists and I denoted it as Spi[d][l] (d is the number of subsets and l is the number of sublists). Spi 中有 3 个子集,每个子集都包含其子列表,我将其表示为 Spi[d][l](d 是子集的数量,l 是子列表的数量)。

I want to enter each subset to insert just one specific number which is an element of S_inter into the 3rd, 4th, and 5th (3, 4, 5 are elements of in_inter_job) position of each sublist respectively.我想输入每个子集以仅将一个作为 S_inter 元素的特定数字插入到每个子列表的第 3、4 和第 5 个(3、4、5 是 in_inter_job 的元素)position 中。

To be more clear, that is inserting number 4 into the first subset, next is number 2 entering to the second subset (2 will be inserted into position 3 of the first sublist, position 4 in the second sublist and 5th position in the last sublist), the do the same for number 6 and the last subset.更清楚地说,就是将数字 4 插入第一个子集,接下来是数字 2 进入第二个子集(2 将插入第一个子列表的 position 3,第二个子列表的 position 4 和第五个子列表的 position 3 ),对数字 6 和最后一个子集执行相同的操作。

S_inter=[4, 2, 6]

in_inter_job=[ 3, 4, 5]

Spi=[[[1, 3, 5, 2, 6], [1, 3, 5, 2, 6], [1, 3, 5, 2, 6]], [[1, 3, 5, 4, 6], [1, 3, 5, 4, 6], [1, 3, 5, 4, 6]], [[1, 3, 5, 4, 2], [1, 3, 5, 4, 2], [1, 3, 5, 4, 2]]]

The result I wanted is like:我想要的结果是这样的:

Spi=[[[1, 3, 5, 4, 2, 6], [1, 3, 5, 2, 4, 6], [1, 3, 5, 2, 6, 4]], [[1, 3, 5, 2, 4, 6], [1, 3, 5, 4, 2, 6], [1, 3, 5, 4, 6, 2]], [[1, 3, 5, 6, 4, 2], [1, 3, 5, 4, 6, 2], [1, 3, 5, 4, 2, 6]]]

But after I run these for loop:但是在我运行这些 for 循环之后:

for d in range(0,len(Spi)):

    for k in S_inter:
        for l, r in zip(range(len(Spi[d])),in_inter_job):
            Spi[d][l].insert(r,k)
            if len(Spi[d][l]) == 6:
                break
print(" After insert ",Spi)

then I get result:然后我得到结果:

Spi=[[[1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 2, 6], [1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 2, 6], [1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 2, 6]], [[1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 4, 6], [1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 4, 6], [1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 4, 6]], [[1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 4, 2], [1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 4, 2], [1, 3, 5, 6, 6, 6, 2, 2, 2, 4, 4, 2]]]

Please help me!请帮我!

Simple nested for loop to insert into each list in Spi at corresponding position in in_inter_job with corresponding value in S_inter:简单的嵌套 for 循环插入到 in_inter_job 中相应 position 的 Spi 中的每个列表中,并在 S_inter 中具有相应的值:

S_inter=[4, 2, 6]
in_inter_job=[ 3, 4, 5]
Spi=[[[1, 3, 5, 2, 6], [1, 3, 5, 2, 6], [1, 3, 5, 2, 6]], [[1, 3, 5, 4, 6], [1, 3, 5, 4, 6], [1, 3, 5, 4, 6]], [[1, 3, 5, 4, 2], [1, 3, 5, 4, 2], [1, 3, 5, 4, 2]]]
for i, x in enumerate(S_inter):
    for j, L in enumerate(Spi[i]):
        pos = in_inter_job[j]
        L.insert(pos, x)

print(Spi)

Your zip (which is similar to what enumerate above is doing) doesn't work, because it doesn't match your logic.您的 zip (与上面的枚举类似)不起作用,因为它不符合您的逻辑。 The positions of in_inter_job do not directly correspond with the indexes in S_inter, and so you end up inserting the wrong values, because you're trying to use the same index for both lists. in_inter_job 的位置与 S_inter 中的索引不直接对应,因此您最终会插入错误的值,因为您尝试对两个列表使用相同的索引。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM