Python Forum
Hashing tuples - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: News and Discussions (https://python-forum.io/forum-31.html)
+--- Thread: Hashing tuples (/thread-22933.html)



Hashing tuples - remow - Dec-03-2019

I just discovered (the bad way) that:
hash((-1,0)) == hash((-2,0))
I rewrote the program using:
hash(str(tuple))
and got it to work.

Has someone a better idea?

Thank you!

(Using python 3.6.9)


RE: Hashing tuples - Gribouillis - Dec-03-2019

I didn't know that. Hash collisions occur. Why is it a problem for you?


RE: Hashing tuples - remow - Dec-03-2019

(Dec-03-2019, 10:36 PM)Gribouillis Wrote: I didn't know that. Hash collisions occur. Why is it a problem for you?

Well... I was trying to speed up an algorithm with an ad-hoc data structure, but it didn't work, also with small datasets. Lost some hours searching for a bug in the wrong places...

I didn't expect to find a collision so fast!


RE: Hashing tuples - Gribouillis - Dec-03-2019

remow Wrote:I didn't expect to find a collision so fast!
hash() is meant to be fast in order to implement hash-table-like algorithms. If you want robust hash functions against collisions, use the hashlib module or cryptographic modules.


RE: Hashing tuples - remow - Dec-03-2019

(Dec-03-2019, 10:51 PM)Gribouillis Wrote: hash() is meant to be fast in order to implement hash-table-like algorithms.

Yes, I liked the fast part and glossed over collisions Wall
Tomorrow I'll try to remove the str() hack and take hash-collisions into account, hoping it won't be too slow/use too much memory with the full dataset (that I still haven't tried with the str() hack, but now I am convinced that it won't work). But now I need sleep.

Thank you!


RE: Hashing tuples - scidam - Dec-04-2019

This is because hash(-2) == hash(-1) and hash of a tuple is based on hashes of its items. So, it is expected that hash((-2,-1,-2)) will be equal to hash((-1,-1,-1)) etc.


RE: Hashing tuples - Gribouillis - Dec-04-2019

Can you explain what you want to do? Instead of the hash() function you could perhaps rely directly on dictionaries.


RE: Hashing tuples - remow - Dec-06-2019

(Dec-04-2019, 04:55 AM)Gribouillis Wrote: Can you explain what you want to do? Instead of the hash() function you could perhaps rely directly on dictionaries.

Thank you, I just did that.
At the end it was not worth the effort, I was reinventing the wheel...

Now I am waiting for the algorithm to finish.