tl/dr
given x = {(a,b),(b,c),(d,e),(b,a),(c,b)} (where x set)
how filter subtuples show unique combination (instead of unique permutation) such x becomes {(a,b),(b,c),(d,e))}
longer form
somewhat of inverse problem of combination/permutation questions on here.
i have set of tuples (outer tuples), each tuple has 2 elements, both of tuples (inner tuples), , each sub-tuple has 2 elements, both of integers.
as example, set 3 elements might like
x = { ( (1,2),(2,3) ), ( (1,3),(1,2) ), ( (2,3),(1,2) ) } while outer tuples unique, i'd build subset contains set of unqiue tuples order of 2 inner tuples irrelevant (i.e. set of unique combinations). in example above reduce to;
x = { ( (1,2),(2,3) ), ( (1,3),(1,2) )} because
( (1, 2),(2,3) ) , ( (2,3),(1,2) ) ) are both combinations of (1, 2) , (2,3).
there obvious brute-force/for-loop approaches don't feel pythonic.
maybe leveraging itertools , map?
you can apply sorted function on elements using map , use set comprehension unique elements :
>>> new={tuple(i) in map(sorted,x)} >>> new set([('b', 'c'), ('a', 'b'), ('d', 'e')]) but note since sorted convert elements list need reverse them tuple because lists not hashable.
Comments
Post a Comment