[SciPy-user] usings numpy arrays in sets

nicky van foreest vanforeest@gmail....
Thu Jun 25 06:12:44 CDT 2009

Dear Francesc,

Thanks. Your second suggestion works really nicely! I also thought
about the first solution, converting arrays to strings, but even emtpy
strings come with a memory penalty of 40 bytes (on my machine).

As an aside I am also exploring pytables. This appears really useful
for me. Hence, another thanks!



2009/6/25 Francesc Alted <faltet@pytables.org>:
> 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
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

More information about the SciPy-user mailing list