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

问题描述

我有一个列表,作为 list< list< list>> 列表,我要删除重复的列表.

I have a lists as list<list<list>> lists where I want to remove duplicate list.

list_1 = [
           [
             [1,2],[2,3]
           ],
           [
             [3,4],[5,6]
           ],
           [
             [3,4], [5,6]
           ]

         ]

因此预期输出应为

output = [
           [
             [1,2],[2,3]
           ],
           [
             [3,4],[5,6]
           ]
         ]

是否有执行此操作的捷径,而不是将每个列表相互比较.我们无法设置(list_1),那么删除重复项最容易的是什么?

Is there are shortcut to do that instead of comparing each list with each other. We cannot do set(list_1), then what is the easiest to remove duplicates?

P.S:这是3级嵌套,因此标记为重复问题的答案不起作用.

P.S: It's a 3 level nesting so the answer marked for duplicate question doesn't work.

推荐答案

您可以尝试

k = [
           [
             [1,2],[2,3]
           ],
           [
             [3,4],[5,6]
           ],
           [
             [3,4], [5,6]
           ]

         ]

import itertools
k.sort()
list(k for k,_ in itertools.groupby(k))
[[[1, 2], [2, 3]], [[3, 4], [5, 6]]]

有关详细信息,请参见 https://stackoverflow.com/a/2213973/4320263

For detailed info you can refer here https://stackoverflow.com/a/2213973/4320263

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

08-28 08:14