找到列表中的非重复元素

def funn(list1):
    for item in list1:
            listA = list1
            listA.remove(item)
            if item not in listA:
                print(item)
funn([1,1,2,3])

我的代码是这样的,但结果扫不出来3,求解
结果

D:\pythonProject3\venv\Scripts\python.exe D:/pythonProject3/练习.py
2

Process finished with exit code 0

def funn(list1):
    for item in list1:
            listA = list1.copy()
            listA.remove(item)
            if item not in listA:
                print(item)
funn([1,1,2,3])

listA 不能直接赋值,因为如果直接赋值,你的listA每次都在变,按照道理来说,你应该是用的原始的list1.如果你不copy()一下,你的remove会改变list1的结果,你可以打印看下

img

如果觉得答案对你有帮助,请点击下采纳,谢谢~

你要删除列表,要从后往前删
否则中间被你删了一个,遍历的时候就把它跳过了