mylist = [[1,2],[4,5],[3,4],[4,3],[2,1],[1,2]]
我想删除重复的项目,重复的项目可以反转。结果应该是:
mylist = [[1,2],[4,5],[3,4]]
我如何在Python中实现这一目标?
答案 0 :(得分:11)
lst=[[1,2],[4,5],[3,4],[4,3],[2,1],[1,2]]
fset = set(frozenset(x) for x in lst)
lst = [list(x) for x in fset]
这不会保留原始列表中的顺序,也不会保留子列表的顺序。
>>> lst=[[1,2],[4,5],[3,4],[4,3],[2,1],[1,2]]
>>> fset = set(frozenset(x) for x in lst)
>>> lst = [list(x) for x in fset]
>>> lst
[[1, 2], [3, 4], [4, 5]]
答案 1 :(得分:2)
如果订单很重要,您可以随时使用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]]
答案 2 :(得分:0)
如果顺序不重要:
def rem_dup(l: List[List[Any]]) -> List[List[Any]]:
tuples = map(lambda t: tuple(sorted(t)), l)
return [list(t) for t in set(tuples)]