简体   繁体   English

从列表python中删除重复项

[英]Remove duplicates from list python

I'm trying to write a program that removes duplicates from a list, but my program keeps throwing the error "list index out of range" on line 5, if n/(sequence[k]) == 1: . 我正在尝试编写一个从列表中删除重复项的程序,但是if n/(sequence[k]) == 1: :,我的程序会在第5行继续抛出错误“list index out of range”。 I can't figure this out. 我无法弄清楚这一点。 Am I right in thinking that the possible values of "k" are 0, 1, and 2? 我是否正确地认为“k”的可能值是0,1和2? How is "sequence" with any of those as the index outside of the possible index range? 如何将“序列”作为可能索引范围之外的索引?

def remove_duplicates(sequence):
    new_list = sequence
    for n in sequence:
        for k in range(len(sequence)):
            if n/(sequence[k]) == 1:
                new_list.remove(sequence[k])
    print new_list

remove_duplicates([1,2,3])

Your error is concurrent modification of the list: 您的错误是并发修改列表:

for k in range(len(sequence)):
    if n/(sequence[k]) == 1:
        new_list.remove(sequence[k])

It may seem removing from new_list shouldn't effect sequence, but you did new_list = sequence at the beginning of the function. 可能看起来从new_list中删除不应该影响序列,但是你在函数的开头做了new_list = sequence This means new_list actually literally is sequence, perhaps what you meant is new_list=list(sequence) , to copy the list? 这意味着new_list实际上字面上是序列,也许你的意思是new_list=list(sequence) ,复制列表?

If you accept that they are the same list, the error is obvious. 如果您接受它们是相同的列表,则错误是显而易见的。 When you remove items, the length, and the indexes change. 删除项目时,长度和索引会更改。

PS As mentioned in a comment by @Akavall, all you need is: PS正如@Akavall在评论中所提到的,您所需要的只是:

sequence=list(set(sequence))

To make sequence contain no dupes. 使序列不包含欺骗。 Another option, if you need to preserve ordering, is: 如果您需要保留订购,另一个选择是:

from collections import OrderedDict
sequence=list(OrderedDict.fromkeys(sequence))

I strongly suggest Akavall's answer: 我强烈建议Akavall回答:

list(set(your_list))

As to why you get out of range errors: Python passes by reference, that is sequence and new_list still point at the same memory location. 至于你为什么超出范围错误:Python通过引用传递,即序列和new_list仍然指向相同的内存位置。 Changing new_list also changes sequence. 更改new_list也会改变顺序。

And finally, you are comparing items with themselves, and then remove them. 最后,您要将项目与自己进行比较,然后将其删除。 So basically even if you used a copy of sequence, like: 所以基本上即使您使用了序列的副本,例如:

new_list = list(sequence)

or 要么

new_list = sequence[:]

It would return an empty list. 它会返回一个空列表。

If you don't like list(set(your_list)) because it's not guaranteed to preserved order, you could grab the OrderedSet recipe and then do: 如果你不喜欢list(set(your_list))因为它不能保证保存顺序,你可以获取OrderedSet配方 ,然后执行:

from ordered_set import OrderedSet

foo = list("face a dead cabbage")
print foo
print list(set(foo)) # Order might change
print list(OrderedSet(foo)) # Order preserved
# like @Akavall suggested
def remove_duplicates(sequence):
    # returns unsorted unique list
    return list(set(sequence))

# create a list, if ele from input not in that list, append.
def remove_duplicates(sequence):
    lst = []
    for i in sequence:
        if i not in lst:
            lst.append(i)
    # returns unsorted unique list
    return lst

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM