I currently have a set like the following:
{(a,b), (b,a), (c,b), (b,c)}
What I Would like to have is:
{(a,b), (c,b)}
As you may notice the duplicate values have been removed completely so that two tuples never have the same elements inside regardless of order.
How can I tell the set to disregard the order of the elements in the tuple and just check the values between the tuples?
Okay, so you've got a set {c1, c2, c3, ...}
, where each cN
is itself a collection of some sort.
If you don't care about the order of the elements in cN
, but do care that it is unique (disregarding order), then cN
should be a frozenset
1 rather than a tuple
:
>>> orig = {("a", "b"), ("b", "a"), ("c", "b"), ("b", "c")}
>>> uniq = {frozenset(c) for c in orig}
>>> uniq
{frozenset(['b', 'a']), frozenset(['b', 'c'])}
As a general rule, choosing an appropriate data type from those provided by Python is going to be more straightforward than defining and maintaining custom classes.
1 It can't be a set
, because as a member of a larger set
it needs to be hashable.