小编Sur*_*nna的帖子

尝试删除Python中的重复项时,只有一个项不会被删除。为什么?

copyofnumbers = [1, 2, 3, 3, 1, 1, 4, 4, 5, 6, 7, 6, 7, 1]
copyofnumbers.sort()

for item in copyofnumbers:
    if (copyofnumbers.count(item) > 1):
        copyofnumbers.remove(item)

print(copyofnumbers)
Run Code Online (Sandbox Code Playgroud)

我正在尝试从列表中删除相同的项目。上面的代码删除所有重复的项目,但不删除“ 1”。我究竟做错了什么 ?

[1, 1, 2, 3, 4, 5, 6, 7]
Run Code Online (Sandbox Code Playgroud)

我希望输出应删除所有重复的项目。

我希望代码删除所有重复的项目。

python

3
推荐指数
1
解决办法
47
查看次数

标签 统计

python ×1