在 "for" 循环中重置列表

List reset in a "for" loop

伙计们,我正在尝试执行此程序,其中我必须获取一个列表并将所有非唯一元素分开并将其放入列表中而不更改其顺序。 例如
[1,2,3,1,3]=[1,3,1,3]
[5,5,5,5]=[5,5,5, 5]
[1,2,3,4,5]=[]

因此,为此,我在 python 中编写了程序,并放置了一些标志以帮助我知道我在做什么 wrong.So 在程序中我尝试重置 list3 值(删除后它的一个元素在一个循环中并退出它)但它不会让我做 it.so 任何帮助将不胜感激。非常感谢你们。

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)