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