删除列表列表中的重复列表

时间:2015-10-08 08:46:29

标签: python list

我有一个列表列表,我想删除列表中的重复项目。

my_list=
[[[2, 5, 71.1], [1, 3, 70.0]],
[[2, 5, 71.1], [1, 3, 70.0]],
[[2, 5, 71.1], [1, 3, 70.0]],
[[5, 10, 80.0], [6, 9, 80.0], [5, 8, 80.0]],
[[5, 10, 80.0], [6, 9, 80.0], [5, 8, 80.0]],
[[5, 10, 80.0], [6, 9, 80.0], [5, 8, 80.0]],
[[10, 12, 80.0]],
[[14, 16, 80.0], [13, 20, 81.0]],
[[14, 16, 80.0], [13, 20, 81.0]],
[[22, 24, 80.0]],
[[26, 28, 80.0], [25, 40, 80.0]],
[[26, 28, 80.0], [25, 40, 80.0]],
[[40, 42, 80.0], [40, 41, 80.0]],
[[40, 42, 80.0], [40, 41, 80.0]],
[[44, 45, 80.0]]]

output_wanted=
 [[[2, 5, 71.1], [1, 3, 70.0]],
 [[5, 10, 80.0], [6, 9, 80.0], [5, 8, 80.0]],
 [[10, 12, 80.0]],
 [[14, 16, 80.0], [13, 20, 81.0]],
 [[22, 24, 80.0]],
 [[26, 28, 80.0], [25, 40, 80.0]],
 [[40, 42, 80.0], [40, 41, 80.0]],
 [[44, 45, 80.0]]]

我需要像设置功能这样快速完成工作的东西。但在这种情况下我不能使用set函数。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

这是因为list不可清除。 在应用list之前,您需要转换tuples set()中的>>> my_list = [[1, 2], [1, 2], [3, 4]] >>> result = [list(el) for el in set(tuple(el) for el in my_list)] [[1, 2], [3, 4]] 元素(可以播放)。

>>> [list(list(y) for y in el) 
        for el in set([tuple(tuple(x) for x in el) for el in my_list])]

[[[26, 28, 80.0], [25, 40, 80.0]],
 [[10, 12, 80.0]],
 [[40, 42, 80.0], [40, 41, 80.0]],
 [[44, 45, 80.0]],
 [[5, 10, 80.0], [6, 9, 80.0], [5, 8, 80.0]],
 [[22, 24, 80.0]],
 [[14, 16, 80.0], [13, 20, 81.0]],
 [[2, 5, 71.1], [1, 3, 70.0]]]

使用您的新数据更新

system / Context @862399