我需要刪除給定列表中的所有唯一對象。爲什麼2和4仍然在2個例子中?
我的代碼通過1,3,4檢查,但沒有通過第二個,它返回[2,4],爲什麼不是[]?
def checkio(data):
for i in data:
if data.count(i) == 1 :
data.remove(i)
return data
if __name__ == "__main__":
assert isinstance(checkio([1]), list), "The result must be a list"
assert checkio([1, 2, 3, 1, 3]) == [1, 3, 1, 3], "1st example"
assert checkio([1, 2, 3, 4, 5]) == [], "2nd example"
assert checkio([5, 5, 5, 5, 5]) == [5, 5, 5, 5, 5], "3rd example"
assert checkio([10, 9, 10, 10, 9, 8]) == [10, 9, 10, 10, 9], "4th example"
是啊,我這樣做了,謝謝!!!! –