Python列表控制流-为什么获胜';t跳过列表中已存在的元素
基本上是试图根据一种模式将数字添加到列表L中 从索引点0开始,打印该索引点的元素。然后,下一个索引点变为刚打印的元素的int,并且模式将继续,直到完成 问题是,如果该索引点已被使用,则向左移动一个索引点,直到到达尚未使用的索引点 下面是我对这个问题的解决方案。一切似乎都正常,即,它沿着数字移动,但当它遇到一个已经使用过的索引点时,它仍会打印该元素 你知道为什么它没有正确地向左跳吗Python列表控制流-为什么获胜';t跳过列表中已存在的元素,python,list,control-flow,Python,List,Control Flow,基本上是试图根据一种模式将数字添加到列表L中 从索引点0开始,打印该索引点的元素。然后,下一个索引点变为刚打印的元素的int,并且模式将继续,直到完成 问题是,如果该索引点已被使用,则向左移动一个索引点,直到到达尚未使用的索引点 下面是我对这个问题的解决方案。一切似乎都正常,即,它沿着数字移动,但当它遇到一个已经使用过的索引点时,它仍会打印该元素 你知道为什么它没有正确地向左跳吗 Output is: If L is: L = [1, 4, 4, 1, 2, 4, 3]
Output is:
If L is: L = [1, 4, 4, 1, 2, 4, 3]
N is: 1, 4, 2, 4, 2, 4, 2] when it should
[1, 4, 2, 4, 1, 4, 3]
# other way around
L = [1, 4, 4, 1, 2, 4, 3]
for index, s in enumerate(L):
print(index, s)
print(' ', L)
M = []
N = []
T = []
i = L[0]
index_position = 0
while len(T) != len(L):
if index_position in T:
index_position = index_position + 1
else:
N.append(i)
T.append(index_position)
index_position = i
i = L[index_position]
print('N is: ', N)
print('T is: ', T)
像这样的
请使用有效变量名。N、 T,L或我很困惑
seen_that_before = []
N = []
for number in L
if number in seen_that_before: continue
seen_that_before.append(number)
N.append(number)