Python:从嵌套列表中删除重复项

2022-01-10 00:00:00 python list duplicates

问题描述

mylist = [[1,2],[4,5],[3,4],[4,3],[2,1],[1,2]]

我想删除重复项,重复项可以反转.结果应该是:

I want to remove duplicate items, duplicated items can be reversed. The result should be :

mylist = [[1,2],[4,5],[3,4]]

如何在 Python 中实现这一点?

How do I achieve this in Python?


解决方案

如果顺序很重要,您可以随时使用 OrderedDict

If the Order Matters you can always use OrderedDict

>>> unq_lst = OrderedDict()
>>> for e in lst:
    unq_lst.setdefault(frozenset(e),[]).append(e)


>>> map(list, unq_lst.keys())
[[1, 2], [4, 5], [3, 4]]

相关文章