简体   繁体   English

如何从列表列表中删除相同的列表?

[英]How to remove identical lists from a list of lists?

I'd like to remove same lists in a list having len(a) = 5 and a = [[1,2,3],[2,3,4], [0,1,2],[2,4,6],[3,6,9]] as results.我想删除具有len(a) = 5a = [[1,2,3],[2,3,4], [0,1,2],[2,4,6],[3,6,9]]作为结果。 How can I get that?我怎么能得到那个?

a1 = [[1,2,3],[2,3,4]]
a2 = [[0,1,2],[2,4,6]]
a3=[[1,2,3],[0,1,2],[3,6,9]]
a = a1+a2+a3
a = [tuple(l) for l in a]
print(set(a))
print(len(a))
a=[list(ele) for ele in a]
print(a)
print(len(a))

You cannot make a set of lists of lists because they are not hashable.您不能创建一组列表列表,因为它们不可散列。 You could first convert them to tuples and then create a set:您可以先将它们转换为元组,然后创建一个集合:

a1 = [[1,2,3],[2,3,4]]
a2 = [[0,1,2],[2,4,6]]
a3=[[1,2,3],[0,1,2],[3,6,9]]
a = a1+a2+a3
a = [list(x) for x in set([tuple(L) for L in a])]

output: output:

[[0, 1, 2], [2, 4, 6], [1, 2, 3], [2, 3, 4], [3, 6, 9]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM