[英]Python changing last elements of list
我有 2 個清單
我想取第一個列表的最后一個元素,如果第二個列表中有任何元素,請將它們移到左側並將元素添加為最后一個。
直到第一個數組中只有第一個元素,所以它會停在:
我對 Python 很陌生,非常感謝一些幫助
您可以為此使用 for 循環。 您可以使用 -1 作為索引值來訪問最后一個元素。
使用列表pop
和索引分配
>>> l1 = ['A','B','C','D','E','F']
>>> l2 = ['X' for _ in range(len(l1))]
>>> print(l1)
['A', 'B', 'C', 'D', 'E', 'F']
>>> print(l2)
['X', 'X', 'X', 'X', 'X', 'X']
>>> for idx in range(len(l1)-1):
p = l1.pop()
l2[idx+1] = p
>>> print(l1)
['A']
>>> print(l2)
['X', 'F', 'E', 'D', 'C', 'B']
您可以使用 python 的內置列表函數完成全部工作,如下所示。
list1 = ['A', 'B', 'C', 'D', 'E', 'F']
list2 = ['X'] * len(list1)
for i in range(len(list) - 1, 0, -1):
item = list1.pop() # Pop the last item of the first list
list2.append(item) # Append it to the end of the second list
list2.remove(0) # Shift once
print(list1) # For Debug
print(list2)
嘗試這個:
l = ['A','B','C','D','E','F']
d = ['X', 'X', 'X', 'X', 'X','X']
for i in range(len(l)-1,0,-1):
t = l.pop()
d[-i] = t
print(l)
print(d)
['A']
['X', 'F', 'E', 'D', 'C', 'B']
這是我可以做到的方式。
first = ['A','B','C','D','E','F']
second = ['X1','X2','X3','X4','X5','X6']
print(first)
print(second)
while True:
if len(first) > 1:
lastElement = first[-1]
first.remove(lastElement)
second.append(lastElement)
second.remove(second[0])
print(first)
print(second)
我在“X”上添加了一個數字,這樣你就可以看到它們是如何移動的。 我使用 [-1] 將列表的最后一個元素保存在變量中,然后使用 remove() 刪除。 與我在第二個列表中添加方法 append 並使用 [0] 刪除第一個元素的方法相同。 正如我在您的示例中看到的,您以 [F,E,D,C,B] 結尾。 在這種情況下,您可以使用 method.sort() 對列表進行排序。 這是 output。
['A', 'B', 'C', 'D', 'E', 'F']
['X1', 'X2', 'X3', 'X4', 'X5', 'X6']
['A', 'B', 'C', 'D', 'E']
['X2', 'X3', 'X4', 'X5', 'X6', 'F']
['A', 'B', 'C', 'D']
['X3', 'X4', 'X5', 'X6', 'F', 'E']
['A', 'B', 'C']
['X4', 'X5', 'X6', 'F', 'E', 'D']
['A', 'B']
['X5', 'X6', 'F', 'E', 'D', 'C']
['A']
['X6', 'F', 'E', 'D', 'C', 'B']
如果我理解了,這可以使工作:
L1 = ["a","b","c","d","e"]
L2 = ["X","X","X","X","X"]
TMP = L1[:]
for i, elmt in enumerate(TMP):
L2[len(TMP)-i-1] = TMP[i]
L1.pop(0)
print(L1)
print(L2)
Output:
[]
['e', 'd', 'c', 'b', 'a']
因此,我想向您展示復雜性和簡單性逐漸增加的答案。 最簡單的答案(也許是最天真的答案)是這樣的:
for i in range(len(list_1)): # loop over every element of the list
if len(list_1) == 1: # when there is only one element left, break from the loop
break
else:
list_2 = list_2[1:len(list_2)] + ['X'] # shift the list one to the left
list_2[len(list_2) - 1] = list_1.pop() # insert the element at the last position
現在稍微觀察一下,您可以看到最后兩個步驟是多余的。 您首先在最后一個 position 中插入一個“X”,只是為了用您實際想要的元素替換它。 所以我們可以通過替換它並內聯下一行的結果來解決這個問題:
for i in range(len(list_1)):
if len(list_1) == 1:
break
else:
# shift the list and insert the last element
list_2 = list_2[1:] + [list_1.pop()]
我還利用了[1:len(list_2)]
- 表示從索引 1 到列表末尾的意思 - 可以重寫為[1:]
的事實。 您不必顯式編寫最后一個元素。
到目前為止,一切都很好。 現在我們可以進一步利用if len(list_1) == 1
只發生在最后一個元素的事實。 因此,換句話說,您可以簡單地省略 for 循環中的最后一個元素,並少循環一次:
for i in range(len(list_1) - 1): # only iterate len(list_1) - 1 times
# shift the list and insert the last element
list_2 = list_2[1:] + [list_1.pop()]
現在一個非常“pythonistic”的想法是,每當你在range(len(...))
的行中寫一些東西時,你就做錯了。 而且,事實上,這個表達式可以進一步簡化。 請注意,我們恰好彈出len(list_1) - 1
次。 通過一些列表操作,這可以重寫為
list_1, temp_list = [list_1[0]], list_1[1:] # list_1 contains now only the first element, temp_list contains all others
list_2[:len(list_1) - len(list_2):-1] = temp_list # use negative indexing to fill list_2 up to the desired position
但它變得更好。 我們可以將第 1 行和第 2 行合並到以下內容:
list_1, list_2[:len(list_1) - len(list_2):-1] = [list_1[0]], list_1[1:]
現在這個單行可能有它的魅力,但會降低可讀性。 所以選擇你喜歡的風格
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.