[SciPy-user] Save a list of arrays

Mathieu Dubois mathieu.dubois@limsi...
Wed Apr 22 12:14:29 CDT 2009


Ryan May wrote:
> On Wed, Apr 22, 2009 at 9:19 AM, Mathieu Dubois 
> <mathieu.dubois@limsi.fr <mailto:mathieu.dubois@limsi.fr>> wrote:
>
>     Pauli Virtanen wrote:
>     > Wed, 22 Apr 2009 15:09:20 +0200, Mathieu Dubois kirjoitti:
>     >
>     >
>     >> [I have posted this message this morning but apparently it is stuck
>     >> somewhere - sorry for multi-posting]
>     >>
>     >> Hello,
>     >>
>     >> I would like to save a list of arrays (each one has a different
>     shape)
>     >> to a file.
>     >> For instance:
>     >>  >>> array1 = numpy.ones(2);
>     >>  >>> array2 = numpy.ones(5);
>     >>  >>> array3 = numpy.ones(1000);
>     >>  >>> list = [array1, array2, array3]
>     >>
>     >> As my arrays are huge (each one contains several thousands
>     values) I
>     >> would like a compressed file.
>     >> numpy.savez would be perfect (because it produces an archive of
>     binary
>     >> files) but unfortunately numpy.savez(list) doesn't work because
>     savez
>     >> needs each array individually.
>     >>
>     >> So what's the best way to do that?
>     >>
>     >
>     >
>     Hello Pauli,
>     > savez(filename, *list)
>     >
>     > The star is Python syntax for unpacking a sequence to arguments
>     Thank you for the tip this opens interesting possibilities.
>
>     Do you know something that works with named arguments (keyword
>     arguments)?
>     This would allow to set the name of the array in the archive file (by
>     default it's 'arr_0.npy').
>
>
> A dictionary and two stars:
>
> arrays = {'a':a, 'b':b, 'c':c}
> savez(filename, **arrays)
Hello Ryan,

Thank you very much this is exactly what I wanted to do. Python is very 
powerful.

Thanks also to Lev and Gabriel but using PyTable and HDF5 would have 
forced me to changed all my scripts...

Kind regards,
Mathieu


More information about the SciPy-user mailing list