繁体   English   中英

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

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

我在 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

基本上如果我的名单是

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 = [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]]

任何帮助表示赞赏。 相关地,我也想删除rows[j-1]但我在第一步完全失败了。 谢谢你。

原始代码:

代码不可重现,所以我以 go 为例。假设我们有一个整数列表列表,我们想用其前身的第一个元素替换每个列表的第一个元素,但前提是替换候选人是偶数。 这是执行此操作的一种方法:

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]]

根据评论编辑 - 见下文

另一方面,如果您想根据受保护索引列表过滤可变列表,则可以像这样简单地完成:

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]] 

注意:如果我们不使用副本,更改将级联,即元素将被先前列表中的更改值替换。

只是为了澄清我对你提供的样本的看法:

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']
]

我的假设是[2, 5]应该是[1, 4] (如果你不想调整它,那么有一个简单的修复方法。)

现在试试

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]

或者(如果你想坚持[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]

要得到

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'
]

那是你要找的吗?

你可以试试这个:

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]

对于rows[j-1]部分,请更详细地说明您需要什么。

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