Search code examples
pythoncombinationspermutationpython-itertools

Get all unique combinations from list of permutations


TL/DR

Given X = {(A,B),(B,C),(D,E),(B,A),(C,B)} (where X is a set)

How do I filter for the subtuples which show a unique combination (instead of a unique permutation) such that X becomes {(A,B),(B,C),(D,E))}

Longer form

Somewhat of an inverse problem from most of the combination/permutation questions on here.

I have a set of tuples (outer tuples), where each tuple has 2 elements, both of which are also tuples (inner tuples), and each sub-tuple has two elements, both of which are integers.

As an example, the set with three elements might look like

X = { ( (1,2),(2,3) ), ( (1,3),(1,2) ), ( (2,3),(1,2) ) }

While all the outer tuples are unique, I'd like to build a subset which contains the set of unqiue tuples where the ORDER of the two inner tuples is irrelevant (i.e. a set of unique combinations). In the example above this would reduce to;

X = { ( (1,2),(2,3) ), ( (1,3),(1,2) )}

Because

( (1, 2),(2,3) ) and ( (2,3),(1,2) ) )

are both just combinations of (1, 2) and (2,3).

There are obvious brute-force/for-loop approaches to this but they don't feel very Pythonic.

Maybe leveraging itertools and map?


Solution

  • You can apply the sorted function on your elements using map and then use a set comprehension to get the unique elements :

    >>> new={tuple(i) for i in map(sorted,X)}
    >>> new
    set([('B', 'C'), ('A', 'B'), ('D', 'E')])
    

    But note that since sorted convert your elements to list you need to reverse them to tuple because lists are not hashable.