Python:如何删除列表列表中的重复列表? [英] Python : How to remove duplicate lists in a list of list?

查看:69
本文介绍了Python:如何删除列表列表中的重复列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从列表列表中删除所有重复列表.

I want to remove all duplicates list from a list of list.

所以我有一个这样的列表.

So I have a list of lists like this.

a = [[1,2],[1,2],[3,4,5],[3,4,5],[3,4,5]]

我想要:

b = [[1,2],[3,4,5]]

我不知道该怎么做.

谢谢

推荐答案

你可以使用一个集合:

b_set = set(map(tuple,a))  #need to convert the inner lists to tuples so they are hashable
b = map(list,b_set) #Now convert tuples back into lists (maybe unnecessary?)

或者,如果您更喜欢列表推导式/生成器:

Or, if you prefer list comprehensions/generators:

b_set = set(tuple(x) for x in a)
b = [ list(x) for x in b_set ]

最后,如果顺序很重要,你总是可以对 b 进行排序:

Finally, if order is important, you can always sort b:

b.sort(key = lambda x: a.index(x) )

这篇关于Python:如何删除列表列表中的重复列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆