Bottom Page

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)
Gribouillis likes this post
Quote
#2
I didn't know that. Hash collisions occur. Why is it a problem for you?
Quote
#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!
Quote
#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.
micseydel likes this post
Quote
#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!
Quote
#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.
Quote
#7
Can you explain what you want to do? Instead of the hash() function you could perhaps rely directly on dictionaries.
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Hashing big files wavic 8 1,180 Apr-06-2018, 07:26 PM
Last Post: wavic

Forum Jump:


Users browsing this thread: 1 Guest(s)