本文翻译自:Removing duplicates in lists
Pretty much I need to write a program to check if a list has any duplicates and if it does it removes them and returns a new list with the items that weren't duplicated/removed. 我几乎需要编写一个程序来检查列表中是否有重复项,如果删除了重复项,则将其删除,并返回一个新列表,其中包含未重复/删除的项。 This is what I have but to be honest I do not know what to do. 这就是我所拥有的,但老实说我不知道该怎么办。
def remove_duplicates():
t = ['a', 'b', 'c', 'd']
t2 = ['a', 'c', 'd']
for t in t2:
t.append(t.remove())
return t
#1楼
参考:https://stackoom.com/question/XP75/删除列表中的重复项
#2楼
>>> t = [1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> t
[1, 2, 3, 1, 2, 5, 6, 7, 8]
>>> s = []
>>> for i in t:
if i not in s:
s.append(i)
>>> s
[1, 2, 3, 5, 6, 7, 8]
#3楼
Another way of doing: 另一种方法:
>>> seq = [1,2,3,'a', 'a', 1,2]
>> dict.fromkeys(seq).keys()
['a', 1, 2, 3]
#4楼
I had a dict in my list, so I could not use the above approach. 我的清单上有一个字典,所以我不能使用上述方法。 I got the error: 我得到了错误:
TypeError: unhashable type:
So if you care about order and/or some items are unhashable . 因此,如果您关心订单和/或某些项目无法散列 。 Then you might find this useful: 然后,您可能会发现这很有用:
def make_unique(original_list):
unique_list = []
[unique_list.append(obj) for obj in original_list if obj not in unique_list]
return unique_list
Some may consider list comprehension with a side effect to not be a good solution. 有些人可能认为列表理解有副作用不是一个好的解决方案。 Here's an alternative: 这是一个替代方案:
def make_unique(original_list):
unique_list = []
map(lambda x: unique_list.append(x) if (x not in unique_list) else False, original_list)
return unique_list
#5楼
Here is an example, returning list without repetiotions preserving order. 这是一个示例,返回不保留重复顺序的列表。 Does not need any external imports. 不需要任何外部进口。
def GetListWithoutRepetitions(loInput):
# return list, consisting of elements of list/tuple loInput, without repetitions.
# Example: GetListWithoutRepetitions([None,None,1,1,2,2,3,3,3])
# Returns: [None, 1, 2, 3]
if loInput==[]:
return []
loOutput = []
if loInput[0] is None:
oGroupElement=1
else: # loInput[0]<>None
oGroupElement=None
for oElement in loInput:
if oElement<>oGroupElement:
loOutput.append(oElement)
oGroupElement = oElement
return loOutput
#6楼
There are also solutions using Pandas and Numpy. 也有使用Pandas和Numpy的解决方案。 They both return numpy array so you have to use the function .tolist()
if you want a list. 它们都返回numpy数组,因此如果需要列表,则必须使用.tolist()
函数。
t=['a','a','b','b','b','c','c','c']
t2= ['c','c','b','b','b','a','a','a']
Pandas solution 熊猫解决方案
Using Pandas function unique()
: 使用熊猫函数unique()
:
import pandas as pd
pd.unique(t).tolist()
>>>['a','b','c']
pd.unique(t2).tolist()
>>>['c','b','a']
Numpy solution 脾气暴躁的解决方案
Using numpy function unique()
. 使用numpy函数unique()
。
import numpy as np
np.unique(t).tolist()
>>>['a','b','c']
np.unique(t2).tolist()
>>>['a','b','c']
Note that numpy.unique() also sort the values . 请注意,numpy.unique()也对值进行排序 。 So the list t2
is returned sorted. 因此,列表t2
被返回排序。 If you want to have the order preserved use as in this answer : 如果您想保留订单,请按照以下答案进行操作 :
_, idx = np.unique(t2, return_index=True)
t2[np.sort(idx)].tolist()
>>>['c','b','a']
The solution is not so elegant compared to the others, however, compared to pandas.unique(), numpy.unique() allows you also to check if nested arrays are unique along one selected axis. 与其他解决方案相比,该解决方案并不那么优雅,但是与pandas.unique()相比,numpy.unique()还可让您检查嵌套数组在一个选定轴上是否唯一。