简体   繁体   English

Python:将列表中列表中的元素替换为列表中的另一个列表中的值

[英]Python: Replace element in list inside list of list, with value from another list in same

I have a list of lists in python in which I want to replace the first value of some lists with the first value of the list preceding it, but only for certain lists.我在 python 中有一个列表列表,我想用它前面的列表的第一个值替换某些列表的第一个值,但仅限于某些列表。

for i, x in enumerate(protected_rows):
        k = protected_rows[i + 1]
        dup_rows_indx = range(x + 2, k, 2) =
        for j in dup_rows_indx:
            try:
            rows[j][0] = rows[j-1][0]
            else:
            continue

basically if my list was基本上如果我的名单是

rows = [[a, b, c], [d], [e, f, g, h, i], [j, k, l, m], [n], [o, p], [q, r, s], [t, u], [v, w, x]]

and protected_rows = [1, 4] I want the output to beprotected_rows = [1, 4]我希望 output 成为

rows = [[a, b, c], [d], [e, f, g, h, i], [e, k, l, m], [n], [o, p], [o, r, s], [t, u], [t, w, x]]

Any help appreciated.任何帮助表示赞赏。 Relatedly I also then want to remove rows[j-1] but I've completely failed at the first step.相关地,我也想删除rows[j-1]但我在第一步完全失败了。 Thank you.谢谢你。

Original Code:原始代码:

The code is not reproducible, so I am making an example as I go. Suppose we have a list of lists of integers, and we want to replace the first element of each list with the first element of its predecessor, but only if the replacement candidate is an even number.代码不可重现,所以我以 go 为例。假设我们有一个整数列表列表,我们想用其前身的第一个元素替换每个列表的第一个元素,但前提是替换候选人是偶数。 Here's one way of doing this:这是执行此操作的一种方法:

import copy

lst = [[1,2,3],[4,5,6],[7],[8,9,10],[11,12,13],[14,15]]

#create a copy to change. 
lst2=copy.deepcopy(lst)
lst2

#iterate over both lists
for l,k in zip(lst, lst2[1:]):
    #print (l,k)

    if(k[0]%2==0):
        k[0]=l[0]

print('====')
print(lst2)

# Out:
# [[1, 2, 3], [1, 5, 6], [7], [7, 9, 10], [11, 12, 13], [11, 15]]

Edit As per Comment- See Below根据评论编辑 - 见下文

If, on the other hand, you want to filter alterable lists based on a list of protected indices, then it can be done simply like this:另一方面,如果您想根据受保护索引列表过滤可变列表,则可以像这样简单地完成:

import copy

lst = [[1,2,3],[4,5,6],[7],[8,9,10],[11,12,13],[14,15]]

protected_rows= [2,4]

#create a copy to change. 
lst2=copy.deepcopy(lst)

#start counting from 1, include final index
for i in range(1,len(lst)):
    if(i not in protected_rows ):
        lst2[i][0]=lst2[i-1][0]
        
print(lst) 
#print(protected_rows)
print(lst2)  
#out:
# [[1, 2, 3], [4, 5, 6], [7], [8, 9, 10], [11, 12, 13], [14, 15]]
# [[1, 2, 3], [1, 5, 6], [7], [7, 9, 10], [11, 12, 13], [11, 15]] 

Note: If we don't use a copy, the changes will cascade, ie the elements will be replaced by the altered value in previous list.注意:如果我们不使用副本,更改将级联,即元素将被先前列表中的更改值替换。

Just for clarification here my view on the sample you've provided:只是为了澄清我对你提供的样本的看法:

rows = [
    ['a', 'b', 'c'],
    ['d'],                      # < protected row (index: 1)
    ['e', 'f', 'g', 'h', 'i'],
    ['j', 'k', 'l', 'm'],
    ['n'],                      # < protected row (index: 4)
    ['o', 'p'],
    ['q', 'r', 's'],
    ['t', 'u'],
    ['v', 'w', 'x']
]

My assumption is that [2, 5] should be [1, 4] .我的假设是[2, 5]应该是[1, 4] (If you don't want to adjust that then there's an easy fix.) (如果你不想调整它,那么有一个简单的修复方法。)

Now try现在试试

protected_rows = [1, 4]
indices = [-1] + protected_rows + [len(rows)]
for i_0, i_1 in zip(indices[:-1], indices[1:]):
    for i in range(i_0 + 2, i_1, 2):
        rows[i][0] = rows[i - 1][0]

or (if you want to stick with [2, 5] )或者(如果你想坚持[2, 5]

protected_rows = [2, 5]
indices = [-1] + [i - 1 for i in protected_rows] + [len(rows)]
for i_0, i_1 in zip(indices[:-1], indices[1:]):
    for i in range(i_0 + 2, i_1, 2):
        rows[i][0] = rows[i - 1][0]

to get要得到

rows = [
    ['a', 'b', 'c'],
    ['d'],                      # < protected row (index: 1)
    ['e', 'f', 'g', 'h', 'i'],  
    ['e', 'k', 'l', 'm'],       # < first item now 'e'
    ['n'],                      # < protected row (index: 4)
    ['o', 'p'],
    ['o', 'r', 's'],            # < first item now 'o'
    ['t', 'u'],
    ['t', 'w', 'x']             # < first item now 't'
]

Is that what you are looking for?那是你要找的吗?

You can try this:你可以试试这个:

rows = [['a', 'b', 'c'], ['d'], ['e', 'f', 'g', 'h', 'i'], ['j', 'k', 'l', 'm'], ['n'], ['o', 'p'], ['q', 'r', 's'], ['t', 'u'], ['v', 'w', 'x']]

protected_rows = [2, 5]

for i in range(len(rows)):
    for j in range(len(protected_rows)):
        if i == protected_rows[j]:
            rows[i+1][0] = rows[i][0]

for the rows[j-1] part please explain in more detail what you need.对于rows[j-1]部分,请更详细地说明您需要什么。

output: output:

[['a', 'b', 'c'], ['d'], ['e', 'f', 'g', 'h', 'i'], ['e', 'k', 'l', 'm'], ['n'], ['o', 'p'], ['o', 'r', 's'], ['t', 'u'], ['v', 'w', 'x']]

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

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