Problem with hash function: hash(1) == hash(1.0)

Posted by mtasic on Stack Overflow See other posts from Stack Overflow or by mtasic
Published on 2009-08-04T16:17:40Z Indexed on 2010/04/27 3:53 UTC
Read the original article Hit count: 607

Filed under:
|
|

I have an instance of dict with ints, floats, strings as keys, but the problem is when there are a as int and b as float, and float(a) == b, then their hash values are the same, and thats what I do NOT want to get because I need unique hash vales for this cases in order to get corresponding values.

Example:

d = {1:'1', 1.0:'1.0', '1':1, '1.0':1.0}
d[1] == '1.0'
d[1.0] == '1.0'
d['1'] == 1
d['1.0'] == 1.0

What I need is:

d = {1:'1', 1.0:'1.0', '1':1, '1.0':1.0}
d[1] == '1'
d[1.0] == '1.0'
d['1'] == 1
d['1.0'] == 1.0

© Stack Overflow or respective owner

Related posts about python

Related posts about hash