使用savez命名numpys关键字,同时使用任意数量的参数

时间:2015-06-15 16:52:55

标签: python numpy

我想用定义的名称保存任意数量的np.arrays。 这是我的例子,考虑到我有三个名字列表,当然还有三个要保存的数组:

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()

输出:

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

那么如何将名单lN映射到我的数组,以正确的名称保存?

1 个答案:

答案 0 :(得分:1)

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

如果要指定名称,请使用关键字类型参数。在这里,我通过扩展(使用**)字典来做到这一点。我也在使用列表推导的新词典版本。