简体   繁体   English

以下“删除列表中的重复项”代码中的问题是什么。 我知道我们可以使用 append() 来做同样的事情

[英]What is the problem in the below code for "removing duplicates in list". I know we can use append() to do the same thing

What is the problem in the below code for "removing duplicates in ~~~list".以下代码中“删除 ~~~list 中的重复项”的问题是什么? I know we can use append() to do the same thing.我知道我们可以使用 append() 来做同样的事情。

    l=0
    numbers = [101,3,7,2,3,5,9,11,11,11,9,4,9,81,6]
    numbers.sort()
    for each_element in numbers:
        if each_element==l:
            l = each_element
            numbers.remove(each_element) 
        else:
            l=each_element
    print(numbers)  

~~~ end of code
As pointed by *Gino Mempin* It is not a good idea to modify the list while iterating through it.
Your code logic is correct but that's not how things are happening here.
Understand by this,
let list is [2, 3, 3, 4, 5, 6, 7, 9, 9, 9, 11, 11, 11, 81, 101],
Now when for loop iterates and detects a duplicate at index 2 then '3' at index 2 is removed.
It's what you wanted it to do but problem is that now '4' comes at index 2 from where '3' is removed. But python for loop has already iterated index 2 which means it don't care about whether there has come a new value and it simply iterate from index 3 now knowingly that the list has been modified.
Because of this it's giving you wrong output from your expectation but in actual it's not a python error but in logic according with python/

If you use the following castings your list won't contain duplication and it will be sorted.如果您使用以下转换,您的列表将不包含重复,并且将被排序。

numbers = sorted(list(set(numbers)))

EDIT:编辑:

Complete implementation:完整实施:

numbers = [101, 3, 7, 2, 3, 5, 9, 11, 11, 11, 9, 4, 9, 81, 6]
sorted_numbers = sorted(list(set(numbers)))
print(sorted_numbers)

Output: Output:

>>> python test.py 
[2, 3, 4, 5, 6, 7, 9, 11, 81, 101]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果我将多个相同的事物添加到列表中,并且想要摆脱其中的一个,我怎么知道它是哪个? - If I append more than one of the same thing to a list and i want to get rid of one of them how do i know which one it is? 我们如何在下面的代码中使用 List Comprehension? - How do we use List Comprehension for the below code? 从列表中删除重复项时遇到问题 - I have a problem removing duplicates from a list 当我使用 readlines 时,我能知道我的简单 python 代码有什么问题吗? - can i know what's problem in my simple python code when i use readlines? 我怎么知道下面的代码是否正确? - How do I know if the code below is correct? 如何在不删除先前相同值的情况下 select 列表中具有重复项的特定数字? - How can I select a specific number in a list with duplicates without removing the previous same values? 有人可以告诉我如何理解下面的python程序(我们使用__iter __()做什么?) - can someone tell me how to understand the python program below(what do we use __iter__() to do?) 从列表中删除重复项,但返回相同列表 - Removing duplicates from a list but returning the same list 那么不能使用 weasyprint 怎么办呢? - So can't use weasyprint what is the best thing to do? 这个程序一遍又一遍地循环同样的事情,我希望它做一次并继续循环下面的其他程序 - This program loops the same thing over and over again and I want it to do it once and continuing on to the other programs below the loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM