Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Hash states to be able to compare them

I have these two states that represent the same state, but with changed order, with Python:

state1 = [ [1,5,6], [8,2,1] ]
state2 = [ [8,2,1], [1,5,6] ]

How can I hash these two states to be able to compare them? (state1 == state2)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I would do it like this:

>>> from collections import Counter
>>> def my_hash(state):
...     counter = Counter([tuple(x) for x in state])
...     items = frozenset(counter.items())
...     return hash(items)
... 
>>> my_hash(state1) == my_hash(state2)
True
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading