从列表中删除重复项的函数| Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35731289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Function to remove duplicates from a List | Python
提问by Aman Dhoot
I am trying to write a function remove_duplicates
to return only unique values from a list input. I tried to come up with some code but it is throwing infinite loop error. I am unable to understand why. The goal is not to achieve result as I have discovered there are direct methods like 'SET' to do this. But, I primarily wanted to understand my mistake as this is my first language and first day at any kind of coding.
我正在尝试编写一个函数remove_duplicates
来仅从列表输入中返回唯一值。我试图想出一些代码,但它抛出了无限循环错误。我不明白为什么。目标不是达到结果,因为我发现有像“SET”这样的直接方法可以做到这一点。但是,我主要想了解我的错误,因为这是我的第一语言,也是任何类型编码的第一天。
def remove_duplicates(x):
z = [x[0]]
for i in range(1,len(x)):
y = i-1
k = 0
while y >= 0:
if x[i] == x[y]:
k = k + 1
y -= 1
else:
break
if k == 0:
z.append(x[i])
return z
回答by svohara
Use the built-in python set capabilities.
使用内置的 python 集功能。
y = list(set(x))
y will be a list of the unique elements of x. This works when the elements in x may be used in a set, so they have to implement __eq__()
and __hash__()
.
y 将是 x 的唯一元素的列表。当 x 中的元素可以在集合中使用时,这会起作用,因此它们必须实现__eq__()
和__hash__()
。
回答by Talat Parwez
It'll be good If you can use
如果你可以使用它会很好
SET operator
SET 运算符
to remove the duplicate elements from the list, like this:
从列表中删除重复元素,如下所示:
my_list = [1, 2, 3, 1, 1, 1, 1, 1, 2, 3, 4]
Now time to remove the duplicate elements from this list:
现在是时候从这个列表中删除重复的元素了:
list(set(my_list))
Answer: [1, 2, 3, 4]
回答by tobias_k
The main problem with your code seem to be here:
您的代码的主要问题似乎在这里:
while y >= 0:
if x[i] == x[y]:
k = k + 1
y -= 1
Here, you decrement y
only if the current element was a match, otherwise you get into an infinite loop. Also, you have to remove the else: break
, otherwise your add-loop will stop right after the first unique element in the list (i.e. after the first element)
在这里,y
仅当当前元素匹配时才递减,否则进入无限循环。此外,您必须删除else: break
,否则您的添加循环将在列表中的第一个唯一元素之后(即在第一个元素之后)停止
If you want to stay true to your initial approach, you could try this:
如果你想忠于你最初的方法,你可以试试这个:
def remove_duplicates(x):
z = [x[0]]
for i in range(1,len(x)):
for y in range(0, i):
if x[i] == x[y]:
break
else:
z.append(x[i])
return z
Note, however, that there are muchsimpler ways to ensure that the elements are unique. For instance, you can just use in
to check whether the current element is already in the result list instead of checking each element individually.
但是请注意,有更简单的方法可以确保元素是唯一的。例如,您可以只使用in
检查当前元素是否已经在结果列表中,而不是单独检查每个元素。
def remove_duplicates(lst):
res = []
for x in lst:
if x not in res:
res.append(x)
return res
If the elements are guaranteed to be hashable, you can also use a set
. But don't do return list(set(lst))
, as this will not preserve the order of the elements in the list. This is a bit more words, but faster than using x not in res
.
如果保证元素是可散列的,您还可以使用set
. 但不要这样做return list(set(lst))
,因为这不会保留列表中元素的顺序。这有点多,但比使用x not in res
.
def remove_duplicates(lst):
seen = set()
res = []
for x in lst:
if x not in seen:
res.append(x)
seen.add(x)
return res
If you want a one-liner like this, you could use OrderedDict
though:
如果你想要这样的单线,你可以使用OrderedDict
:
import collections
def remove_duplicates(lst):
return collections.OrderedDict(zip(lst, lst)).values()