Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need an easy way to remove duplicates of nested tuples in python

Tags:

python

tuples

I am currently working with a script that has lists that looks like this:

example = [ ((2,1),(0,1)), ((0,1),(2,1)), ((2,1),(0,1)) ]

Now turning this list to a set returns:

set( [ ((2,1),(0,1)), ((0,1),(2,1)) ] )

For my purposes I need to recognize these tuples as being equal as well. I dont care about retaining the order. All solutions I can think of is really messy so if anyone has any idea I would be gratefull.

like image 479
monostop Avatar asked Dec 22 '22 09:12

monostop


1 Answers

It sounds like you may be off using frozensets instead of tuples.

>>> x = [((2, 1), (0, 1)), ((0, 1), (2, 1)), ((2, 1), (0, 1))]
>>> x
[((2, 1), (0, 1)), ((0, 1), (2, 1)), ((2, 1), (0, 1))]
>>> set(frozenset(ts) for ts in x)
set([frozenset([(0, 1), (2, 1)])])
like image 193
Mike Graham Avatar answered Dec 24 '22 02:12

Mike Graham