user69453
user69453

Reputation: 1405

Name numpys keywords with savez while using arbitrary number of arguments

I want to save a arbitrary number of np.arrays with an defined name. Here is my example, considering I have a name list of three and (of course) three arrays to save:

import numpy as np

l = [np.random.random_integers(5, size = (3., 2.)), np.random.random_integers(5, size = (3., 2.)), np.random.random_integers(5, size = (3., 2.))]
lN = ['a', 'b', 'c']

a = np.savez('test.npz', *[l for i in l])
b = np.load('test.npz')
print b.keys()

Output:

['arr_1', 'arr_0', 'arr_2']

So how do I map the namelist lN to my arrays, to be saved with the correct name?

Upvotes: 2

Views: 839

Answers (1)

hpaulj
hpaulj

Reputation: 231475

np.savez('test.npz',**{name:value for name,value in zip(lN,l)})

If you want to specify the names, use the keyword type of parameter. Here I am doing that by expanding (with **) a dictionary. I'm also using the newer dictionary version of a list comprehension.

Upvotes: 6

Related Questions