# Python从给定列表中删除重复的子列表

2021年4月26日16:31:41 发表评论 737 次浏览

``````Input : [[1], [1, 2], [3, 4, 5], [2, 1]]
Output : [[1], [1, 2], [3, 4, 5]]

Input : [['a'], ['x', 'y', 'z'], ['m', 'n'], ['a'], ['m', 'n']]
Output : [['a'], ['x', 'y', 'z'], ['m', 'n']]``````

``````# Python3 program to Remove repeated
# unordered sublists from list

def Remove(lst):
return ([ list (i) for i in { * [ tuple ( sorted (i)) for i in lst]}])

# Driver code
lst = [[ 1 ], [ 1 , 2 ], [ 3 , 4 , 5 ], [ 2 , 1 ]]
print (Remove(lst))``````

``[[1, 2], [3, 4, 5], [1]]``

``````# Python3 program to Remove repeated
# unordered sublists from list

def Remove(lst):
return list ( map ( list , ( set ( map ( lambda x: tuple ( sorted (x)), lst)))))

# Driver code
lst = [[ 1 ], [ 1 , 2 ], [ 3 , 4 , 5 ], [ 2 , 1 ]]
print (Remove(lst))``````

``[[1, 2], [3, 4, 5], [1]]``

``````# Python3 program to Remove repeated
# unordered sublists from list

def Remove(lst):
res = []
check = set ()

for x in lst:
hsh = tuple ( sorted (x))
if hsh not in check:
res.append(x)
``[[1], [1, 2], [3, 4, 5]]``