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

标签 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 函数这样可以快速完成工作的东西。但在这种情况下我无法使用 set 函数。有什么办法可以做到吗?

最佳答案

这是因为 list 不可散列。 在应用 set() 之前,您需要将 list 的元素转换为 tuples(可散列)。

>>> 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]]]

关于python - 删除列表中的重复列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33010861/

相关文章:

python - 在 python 中用函数参数命名的列表

python - 在 Python 中,如何在保留词序的同时从两个列表中找到常用词?

c++ - 模板特化站点报告 "too few template-parameter-lists"错误

python - 如何关闭用于运行方法的线程?

python - 在遍历 n 维 numpy 数组时创建新数组

python - 在 python 中的电子邮件的发件人字段中添加发件人姓名

python - 根据字符串内容对字符串列表进行排序

python - 如何在 python 的主函数中运行函数?

python - 如何从请求 python 的响应中检索文本

c# - 获取文件信息列表中的文件名列表