python列表去重
原始列表:
[[2,2,3],[2,3,2],[3,2,2],[7]]
去重后的列表:
[[2,2,3],[7]]
列表里的元素不分顺序,只是把重复的列表去掉,只保留一个
该如何做啊
回答:
只能做到这种程度了
➜ ~ ipythonPython 3.7.4 (default, Sep 7 2019, 18:27:02)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: x = [[2,2,3],[2,3,2],[3,2,2],[7]]
In [2]: [list(_) for _ in set([tuple(sorted(__)) for __ in x])]
Out[2]: [[7], [2, 2, 3]]
回答:
list1=[[2,2,3],[2,3,2],[3,2,2],[7]]list2=[]
list3=[]
for item in list1:
item.sort()
arr=[]
for item2 in item:
arr.append(str(item2))
str1='-'.join(arr)
if not (str1 in list2):
list2.append(str1)
list3.append(item)
print(list3)
回答:
FP
from functools import reduceraw = [[2, 2, 3], [2, 3, 2], [3, 2, 2], [7]]
dst = reduce(lambda a, c: a if c in a else a + [c], map(sorted, raw), [])
print(list(dst))
# [[2, 2, 3], [7]]
以上是 python列表去重 的全部内容, 来源链接: utcz.com/a/161561.html