从元组列表中删除排列 [英] Removing permutations from a list of tuples

查看:76
本文介绍了从元组列表中删除排列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对这个问题的任何帮助表示赞赏.

我有一个元组列表

a = [(1,2), (2,1), (1,3), (1,4), (4,1)]

并且我需要删除某种类型的重复项:根据我的定义,(1,2) 和 (2,1) 被认为是重复项.所需输出

a = [(1,2), (1,3), (1,4)]

提前致谢

解决方案

您可以对它们进行排序,然后使用 set() 删除重复项:

<预><代码>>>>set(tuple(sorted(l)) for l in a)设置([(1, 2), (1, 3), (1, 4)])

Any help with this question is appreciated.

I have a list of tuples

a = [(1,2), (2,1), (1,3), (1,4), (4,1)]

and I need to remove duplicates of a certain type: (1,2) and (2,1) are considered duplicates according to my definition. Required output

a = [(1,2), (1,3), (1,4)]

Thanks in advance

解决方案

You could sort them and then remove duplicates with set():

>>> set(tuple(sorted(l)) for l in a)
    set([(1, 2), (1, 3), (1, 4)])

这篇关于从元组列表中删除排列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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