Python 列表重置在“a”中;至于;环
伙计们,我正在尝试做这个程序,我必须得到一个列表,分离所有非唯一的元素,并把它放在一个列表中,而不改变它的顺序。 例如Python 列表重置在“a”中;至于;环,python,list,for-loop,reset,Python,List,For Loop,Reset,伙计们,我正在尝试做这个程序,我必须得到一个列表,分离所有非唯一的元素,并把它放在一个列表中,而不改变它的顺序。 例如[1,2,3,1,3]=[1,3,1,3][5,5,5]=[5,5,5][1,2,3,4,5]=[] 为此,我用python编写了这个程序,并放置了一些标志来帮助我知道哪里做错了。因此在程序中,我尝试重置list3值(在删除循环中的一个元素并退出它之后),但它不允许我这么做。因此,任何帮助都将不胜感激。非常感谢你们 list1=[1,2,3,1,3] list2=[] d=0 m
[1,2,3,1,3]=[1,3,1,3]
[5,5,5]=[5,5,5]
[1,2,3,4,5]=[] 为此,我用python编写了这个程序,并放置了一些标志来帮助我知道哪里做错了。因此在程序中,我尝试重置list3值(在删除循环中的一个元素并退出它之后),但它不允许我这么做。因此,任何帮助都将不胜感激。非常感谢你们
list1=[1,2,3,1,3]
list2=[]
d=0
m=0
l=len(list1)
print(l)
for x in range(0,l):
print('for loop')
list3=list1 # I used this to reset the list3 to list1 and use it in the loop but it wouldn't reset
print('this is the reset of list 3',list3)
m=list1[x]
print(m,x)
print(list3[x])
del (list3[x])
while True:
print('while loop',list3[d])
if m==list3[d]:
print('its here')
list2.append(m)
print('this is list2: ',list2)
d+=1
if d==lenght(list3)-1:
print('its here22222')
break
print(list2)
# my results were
5
for loop
this is the reset of list 3 [1, 2, 3, 1, 3]
1 0
1
while loop 2
while loop 3
while loop 1
its here
this is list2: [1]
its here22222
for loop
this is the reset of list 3 [2, 3, 1, 3]
3 1
IndexError: list index out of range
这是你想要的吗
#list1=[5,5,5,5,5]
list1=[1,2,3,1,3]
#list1=[1,2,3,4,5]
list2=[]
for num in list1:
if list1.count(num) > 1:
list2.append(num)
print(list2)
list3=list1没有复制list1,请查看它是否正常工作。我知道计数函数。我也用过。但是我不明白为什么第9行的list3没有将list3重置为list1。非常感谢@dovo36.:)哦,好的。正如paisonco上面所说的。list3=list1使list3指向与list1相同的内存。因此,它们是同一事物的两个不同标签。如果你想复制列表1,覆盖列表3,然后阅读他给你的链接,它显示了不同的方法。非常感谢@davo36。这真的帮了我大忙