Python:如果包含/不包含顺序相同,则从列表中删除子列表 [英] Python: Remove Sublists from List if Same Including/Not Including Order

查看:88
本文介绍了Python:如果包含/不包含顺序相同,则从列表中删除子列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以从列表列表中删除重复的子列表,即使它们的排序不相同?

Is there a way to remove duplicate sublists from a list of lists, even if they are not the same ordering?

所以我可以做些类似的事情:

So could I do something like make:

x = [[1,2],[3,4],[5,6],[2,1],[7,8]]

进入

x = [[1,2],[3,4],[5,6],[7,8]]

有itertools函数或带有for循环的东西吗?

Is there an itertools function or something with a for loop?

谢谢!

推荐答案

这将保留列表和子列表的顺序,并可能在子列表中重复:

this will preserve the order of list and sublists, with possible duplicates in sublists:

y, s = [], set()
for t in x:
    w = tuple(sorted(t))
    if not w in s:
        y.append(t)
        s.add(w)

如果

x = [[1,2],[3,4],[5,6],[2,1,1],[2,1],[7,8],[4,3],[1,2,1]]

那么y将是:

[[1, 2], [3, 4], [5, 6], [2, 1, 1], [7, 8]]

这篇关于Python:如果包含/不包含顺序相同,则从列表中删除子列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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