Floats as keys in dict

greg greg at cosc.canterbury.ac.nz
Wed Aug 1 22:19:59 EDT 2007


Brian Elmegaard wrote:
> However, the dict keys are then floats and I have to round the values
> of new possible nodes in each step.

Be careful with this. If you have two values that are
very close together, but on different sides of a rounding
boundary, they will end up as distinct keys even though
they "should" be regarded as equal.

I don't think there's any way to use a dictionary for
this that works properly in all cases. Whatever scheme
you come up with for comparison and hashing will suffer
from this problem one way or another.

So, like Alex said, you need a different data structure.

--
Greg



More information about the Python-list mailing list