小编Cry*_*era的帖子

从Python列表中删除重复项

我正在尝试编写一个从列表中删除重复项的程序,但我的程序在第 5 行不断抛出错误“列表索引超出范围” if n/(sequence[k]) == 1:。我无法弄清楚这一点。我认为“k”的可能值为 0、1 和 2 是否正确?将其中任何一个作为可能索引范围之外的索引的“序列”如何?

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])
Run Code Online (Sandbox Code Playgroud)

python list

6
推荐指数
1
解决办法
1660
查看次数

标签 统计

list ×1

python ×1