如果我有这样一个列表:
mylist = [[1,2,3], ['a', 'c'], [3,4,5],[1,2], [3,4,5], ['a', 'c'], [3,4,5], [1,2]]
删除重复子列表的最佳方法是什么?
现在我使用 this :
y, s = [ ], set( )
for t in mylist:
w = tuple( sorted( t ) )
if not w in s:
y.append( t )
s.add( w )
可以,但是不知道有没有更好的办法?更像 Python 的东西?
请您参考如下方法:
将元素转换为元组*,然后将其全部转换为集合,然后将所有内容转换回列表:
m = [[1,2,3], ['a', 'c'], [3,4,5],[1,2], [3,4,5], ['a', 'c'], [3,4,5], [1,2]]
print [list(i) for i in set(map(tuple, m))]
*我们正在转换为元组,因为列表是不可散列的(因此我们不能对它们使用集合
