Get all unique combinations from a permutation list


Alex

TL / DR

Given X = {(A,B),(B,C),(D,E),(B,A),(C,B)}( Xone of set)

How to filter the sub-tuple which shows a unique combination (rather than a unique combination) so that it Xbecomes{(A,B),(B,C),(D,E))}

longer form

Most combination/permutation problems here have some kind of inverse problem.

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

For example, a collection with three elements might look like

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

Although all outer tuples are unique, I want to build a subset of non-queue tuples (i.e. a set of unique combinations) that are ORDER unrelated of the two inner tuples. 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) ) )

Yes both are only combined (1, 2)sums (2,3).

There are obvious brute force/loop methods, but they don't feel very Pythonic.

Maybe take advantage of itertoolsand map?

Caslavin

You can use apply a sortedfunction to elements mapand then use a set comprehension to get unique elements:

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

Note, however, that since sortedyou convert the elements to a list, you need to reverse it to a tuplelist , because lists are not hashable.

Related


Get all unique combinations from a permutation list

Alex TL / DR Given X = {(A,B),(B,C),(D,E),(B,A),(C,B)}( Xone of set) How to filter the sub-tuple which shows a unique combination (rather than a unique combination) so that it Xbecomes{(A,B),(B,C),(D,E))} longer form Most combination/permutation problems here

Get all unique combinations from a permutation list

Alex TL / DR Given X = {(A,B),(B,C),(D,E),(B,A),(C,B)}( Xone of set) How to filter the sub-tuple which shows a unique combination (rather than a unique combination) so that it Xbecomes{(A,B),(B,C),(D,E))} longer form Most combination/permutation problems here

Get unique combinations sequentially from a non-unique list

BVDev I have a problem and just can't wrap my head around it, hope someone can help me. Imagine the following situation: There is a list of all people with names. So everyone is an object with a "name" property. From this list, a random number of people are ch

Get unique combinations sequentially from a non-unique list

BVDev I have a problem and just can't wrap my head around it, hope someone can help me. Imagine the following situation: There is a list of all people with names. So everyone is an object with a "name" property. From this list, a random number of people are ch

Get unique combinations sequentially from a non-unique list

BVDev I have a problem and just can't wrap my head around it, hope someone can help me. Imagine the following situation: There is a list of all people with names. So everyone is an object with a "name" property. From this list, a random number of people are ch

Get unique combinations sequentially from a non-unique list

BVDev I have a problem and just can't wrap my head around it, hope someone can help me. Imagine the following situation: There is a list of all people with names. So everyone is an object with a "name" property. From this list, a random number of people are ch

Get unique combinations sequentially from a non-unique list

BVDev I have a problem and just can't wrap my head around it, hope someone can help me. Imagine the following situation: There is a list of all people with names. So everyone is an object with a "name" property. From this list, a random number of people are ch

Get unique combinations sequentially from a non-unique list

BVDev I have a problem and just can't wrap my head around it, hope someone can help me. Imagine the following situation: There is a list of all people with names. So everyone is an object with a "name" property. From this list, a random number of people are ch

Get unique combinations sequentially from a non-unique list

BVDev I have a problem and just can't wrap my head around it, hope someone can help me. Imagine the following situation: There is a list of all people with names. So everyone is an object with a "name" property. From this list, a random number of people are ch

Get unique combinations sequentially from a non-unique list

BVDev I have a problem and just can't wrap my head around it, hope someone can help me. Imagine the following situation: There is a list of all people with names. So everyone is an object with a "name" property. From this list, a random number of people are ch

Get unique combinations sequentially from a non-unique list

BVDev I have a problem and just can't wrap my head around it, hope someone can help me. Imagine the following situation: There is a list of all people with names. So everyone is an object with a "name" property. From this list, a random number of people are ch

How to get all combinations using conditional permutation

username from itertools import permutations permList = permutations('ABC') for perm in list(permList): print (''.join(perm)) output ABC ACB BAC BCA CAB CBA How can I include getting a combination of 2 letters along with the above output, basically

How to get all combinations using conditional permutation

username from itertools import permutations permList = permutations('ABC') for perm in list(permList): print (''.join(perm)) output ABC ACB BAC BCA CAB CBA How can I include getting a combination of 2 letters along with the above output, basically

How to get all combinations using conditional permutation

username from itertools import permutations permList = permutations('ABC') for perm in list(permList): print (''.join(perm)) output ABC ACB BAC BCA CAB CBA How can I include getting a combination of 2 letters along with the above output, basically

How to get all combinations using conditional permutation

username from itertools import permutations permList = permutations('ABC') for perm in list(permList): print (''.join(perm)) output ABC ACB BAC BCA CAB CBA How can I include getting a combination of 2 letters along with the above output, basically

Get all combinations from list values

Chinnon I am using python 2.7, I have the following list: new_out_filename = ['OFF_B8', 0, 'ON_B8', 1, 'ON_B16', 4, 'OFF_B0', 7] I want to get all combinations of strings like this OFF_B8_vs_ON_B8, OFF_B8_vs_ON_B16, OFF_B8_vs_OFf_B0, ON_B8_vs_ON_16, etc. Is t

Get all combinations from list values

Chinnon I am using python 2.7, I have the following list: new_out_filename = ['OFF_B8', 0, 'ON_B8', 1, 'ON_B16', 4, 'OFF_B0', 7] I want to get all combinations of strings like this OFF_B8_vs_ON_B8, OFF_B8_vs_ON_B16, OFF_B8_vs_OFf_B0, ON_B8_vs_ON_16, etc. Is t

Get all combinations from list values

Chinnon I am using python 2.7, I have the following list: new_out_filename = ['OFF_B8', 0, 'ON_B8', 1, 'ON_B16', 4, 'OFF_B0', 7] I want to get all combinations of strings like this OFF_B8_vs_ON_B8, OFF_B8_vs_ON_B16, OFF_B8_vs_OFf_B0, ON_B8_vs_ON_16, etc. Is t

Get a sublist of n elements from a unique permutation of a list

optimized bytes I am writing code to roll dice with different side counts together. The main function accepts a list of 2 numbers that represent the sides of the dice, for example [4, 6]- meaning the program will do things with a d4and a d6. Here is the proble