Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hashing tuples
#1
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)
Reply
#2
I didn't know that. Hash collisions occur. Why is it a problem for you?
Reply
#3
(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!
Reply
#4
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.
Reply
#5
(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!
Reply
#6
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.
Reply
#7
Can you explain what you want to do? Instead of the hash() function you could perhaps rely directly on dictionaries.
Reply
#8
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Hashing big files wavic 8 4,410 Apr-06-2018, 07:26 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020