[英]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.