本文介绍了删除与生成器Python 3.5中具有相同元素的顺序无关的重复元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个元组生成器,我需要删除包含相同元素的元组.我需要此输出进行迭代.
I have a generator of tuples and I need to delete tuples containing same elements. I need this output for iterating.
Input = ((1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3))
Output= ((1, 1), (1, 2), (1, 3))
输出顺序无关紧要.
我已经检查了此问题,但它与列表有关:
I have checked this question but it is about lists: Delete duplicate tuples with same elements in nested list Python
由于数据非常大,我使用生成器来获得最快的结果.
I use generators to achieve fastest results as the data is very large.
推荐答案
您可以通过对数据进行排序来规范化数据,然后将其添加到数据集中以删除重复项
You can normalize the data by sorting it, then add it to a set to remove duplicates
>>> Input = ((1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3))
>>> Output = set(tuple(sorted(t)) for t in Input)
>>> Output
{(1, 2), (1, 3), (2, 3), (1, 1), (3, 3)}
这篇关于删除与生成器Python 3.5中具有相同元素的顺序无关的重复元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!