[SciPy-User] Smart Hashing of Integer Numbers

Robert Kern robert.kern at gmail.com
Thu Sep 24 15:34:43 EDT 2009


On Thu, Sep 24, 2009 at 12:19, nicky van foreest <vanforeest at gmail.com> wrote:
> Hi,
>
> i also struggled with a similar problem for a while. I wanted to use
> arrays as indices to rows of a sparse matrix. For this purpose I
> needed the smallest possible unique integer for each new array.
>
> My solution is like this (with help of the scipy community of course).
> Cast the array to a dictionary key (details below), and use this key
> in a dict to map it to a number equal to the length of the dict. This
> has two nice properties:  the mapping is a bijection, and the size of
> the target int is minimal.
>
> In some more detail:
>
> yourArray = [2,3]
>
> key = array(yourArray, dtype=int8) # i only needed very small ints in the array
> key.flags.writeable = False
>
> index = dict()
>
> # add keys like this:
>
> if key not in index:
>     index[key] = len(index)
>
>
> Beware about the trick with setting the flags. Francesc Alted
> suggested me this solution, but Robert Kern had some objections
> against this (see the mailing list) which were just a bit too
> difficult for me to grasp.

Well, that code certainly doesn't work:

In [1]: key = array([1, 2], dtype=uint8)

In [2]: key.flags.writeable = False

In [3]: hash(key)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/rkern/<ipython console> in <module>()

TypeError: unhashable type: 'numpy.ndarray'

In [4]: index = {}

In [5]: key in index
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/rkern/<ipython console> in <module>()

TypeError: unhashable type: 'numpy.ndarray'

In [6]: index[key] = len(index)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/rkern/<ipython console> in <module>()

TypeError: unhashable type: 'numpy.ndarray'


Did you actually mean something else?

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the SciPy-User mailing list