[SciPy-user] usings numpy arrays in sets

Francesc Alted faltet@pytables....
Thu Jun 25 05:42:49 CDT 2009


A Thursday 25 June 2009 10:36:03 nicky van foreest escrigué:
> Hi,
>
> I need to store numpy arrays in a python set. This is of course not
> possible right away as arrays are not hashable. One way to achieve
> this is to convert arrays to tuples, and use these tuples instead.
> However, tuples need much more memory than arrays, which for my
> purposes is undesirable. Does anybody know of a method to store numpy
> arrays in a set (or something that gives similar behavior)?

One possibility is to convert your arrays into strings by using the 
`tostring()` method.  Then you can reconstruct your arrays with 
`fromstring()`.  However, you will need to add type and shape information 
during the reconstruction process.

Another possibility in case you want to avoid a copy of the array data buffer 
is to turn your arrays read-only (arr.flags.writeable=False), and use the data 
buffer `arr.data` as the entry for the set.  To reconstruct the array, you 
will have to use `frombuffer()` and add type and shape information afterwards, 
as above.  That should be pretty efficient, but the retrieved arrays will 
remain read-only.

Hope that helps,

-- 
Francesc Alted


More information about the SciPy-user mailing list