Hashing in python
MRAB
python at mrabarnett.plus.com
Sat Feb 13 19:47:18 EST 2010
Vish wrote:
> Hi,
>
> I need to hash 3d coordinates to a grid which has been divided into
> 4*4*4 squares. Using python, I thought of a simple way as follows:
>
> CELL_SIZE = 4
>
> def key(point):
>
> return (
> int((floor(point[0]/CELL_SIZE))*CELL_SIZE),
> int((floor(point[1]/CELL_SIZE))*CELL_SIZE),
> int((floor(point[2]/CELL_SIZE))*CELL_SIZE)
> )
>
>
> Since python allows keys to be tuples, I think that this should work.
> Is there a better (more efficient) way to do it?
>
floor(x) returns an integer, so floor(x/CELL_SIZE)*CELL_SIZE is an
integer multiple of CELL_SIZE, also an integer. There's actually no
point (unintentional pun!) to multiplying by CELL_SIZE:
CELL_SIZE = 4
def key(point):
return (
floor(point[0] / CELL_SIZE),
floor(point[1] / CELL_SIZE),
floor(point[2] / CELL_SIZE)
)
More information about the Python-list
mailing list