我有一个包含元组列表的列表,如下所示。
mylist = [['xxx', 879], ['yyy', 315], ['xxx', 879], ['zzz', 171], ['yyy', 315]]
我想从mylist
中删除重复的元组并获得如下输出。
mylist = [['xxx', 879], ['yyy', 315], ['zzz', 171]]
好像python中的set
不适合它。
mylist = list(set(mylist))
有没有快速简便的方法在python中执行此操作(可能使用库)?
您需要编写保留第一个子列表的代码,然后删除其余的。最简单的方法是反转mylist
,将其加载到dict
对象中,并再次将其键值对检索为列表。
>>> list(map(list, dict(mylist).items()))
或者,使用列表理解 -
>>> [list(v) for v in dict(mylist).items()]
[['zzz', 171], ['yyy', 315], ['xxx', 879]]
注意,这个答案不维持秩序!此外,如果您的子列表可以包含两个以上的元素,那么像@JohnJosephFernandez' answer所示,一种涉及散列数据的tuplized版本的方法将是最好的做法。
您无法执行此操作的原因是因为您有一个列表列表而不是元组列表。
你能做的是:
mytuplelist = [tuple(item) for item in mylist]
mylist = list(set(mytuplelist))
要么
mylist = list(set(map(tuple, mylist)))
看起来你想保留秩序。在这种情况下,您可以保留一个跟踪已添加列表的集合。
这是一个例子:
mylist = [['xxx', 879], ['yyy', 315], ['xxx', 879], ['zzz', 171], ['yyy', 315]]
# set that keeps track of what elements have been added
seen = set()
no_dups = []
for lst in mylist:
# convert to hashable type
current = tuple(lst)
# If element not in seen, add it to both
if current not in seen:
no_dups.append(lst)
seen.add(current)
print(no_dups)
哪些输出:
[['xxx', 879], ['yyy', 315], ['zzz', 171]]
注意:由于列表不可清除,您可以将元组添加到seen
集。
另外一个选项:
>>> mylist = [['xxx', 879], ['yyy', 315], ['xxx', 879], ['zzz', 171], ['yyy', 315]]
>>> y = []
>>> for x in mylist:
... if not x in y:
... y+=[x]
...
>>> y
[['xxx', 879], ['yyy', 315], ['zzz', 171]]