使用任意数量的参数时,用savez命名numpys关键字 [英] Name numpys keywords with savez while using arbitrary number of arguments

查看:133
本文介绍了使用任意数量的参数时,用savez命名numpys关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用定义的名称保存任意数量的np.array. 这是我的示例,考虑到我要保存三个和(当然)三个数组的名称列表:

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

输出:

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

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

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

推荐答案

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.

这篇关于使用任意数量的参数时,用savez命名numpys关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆