Delete duplicate tuples independent of order with same elements in generator Python 3.5

前端 未结 1 1557
余生分开走
余生分开走 2021-01-29 13:43

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,         


        
相关标签:
1条回答
  • 2021-01-29 14:16

    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)}
    
    0 讨论(0)
提交回复
热议问题