python列表去重

原始列表:
[[2,2,3],[2,3,2],[3,2,2],[7]]

去重后的列表:
[[2,2,3],[7]]

列表里的元素不分顺序,只是把重复的列表去掉,只保留一个
该如何做啊

阅读 2.2k
3 个回答

只能做到这种程度了

➜  ~ ipython
Python 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 reduce

raw = [[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]]
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题