Reputation: 307
Suppose I have a list of tuples:
a = [(a,b), (a,b), (b,c), (b,a), (a,b), (c,b)]
How can I find all the 'reversed' duplicated tuples like (a,b)
and (b,a)
, (b,c)
and (c,b)
; and change elements inside tuples in a consistent order, so it can become:
a = [(a,b), (a,b), (b,c), (a,b), (a,b), (b,c)]
Upvotes: 0
Views: 319
Reputation: 700
Short answer
Convert tuples to list, sort the list, return it to a tuple type.
def sort_tuples(alistoftuples):
return [tuple(sorted(k)) for k in alistoftuples]
Upvotes: 3