![](/img/trans.png)
[英]Efficient creation of numpy arrays from list comprehension and in general
[英]creation of numpy array of arrays from list
我想从一个列表(下面的代码中的nmax)创建一个数组数组。 我正在尝试使用列表理解,但没有得到预期的结果。 如果我手动分配行,它将按预期工作:
dummy[0] = np.linspace(1, nmax[0], len(nmax))
dummy[0]
array([ 1. , 1.00166722, 1.00333444, ..., 5.99666556,
5.99833278, 6. ])
dummy[2999] = np.linspace(1, nmax[2999], len(nmax))
dummy[2999]
array([ 1. , 1.01433811, 1.02867623, ..., 43.97132377,
43.98566189, 44. ])
但是通过列表理解,它给了我不同的结果:
dummy = [(np.linspace(1, nmax[i], len(nmax))) for i in nmax]
dummy[0]
array([ 1. , 1.00166722, 1.00333444, ..., 5.99666556,
5.99833278, 6. ])
dummy[2999]
array([ 1. , 1.00200067, 1.00400133, ..., 6.99599867,
6.99799933, 7. ])
从一个值到另一个值的变化是在索引2585上,我不知道为什么。
这是完整的代码:
rho=7800
lamb = 500 * 10 **(-9)
#parameters
a = np.linspace(1e-9, 3000e-9, 3000)
x = a * 2*np.pi / lamb
nmax = [int(round(2+i+4*i**(1/3))) for i in x] # list
n = np.zeros((len(nmax), len(nmax)))
#dummy = [(np.linspace(1, nmax[i], len(nmax))) for i in nmax]
尝试用dummy = [(np.linspace(1, i, len(nmax))) for i in nmax]
,您实际上正在做的是dummy[2999] = np.linspace(1, nmax[nmax[2999]], len(nmax))
而不是dummy[2999] = np.linspace(1, nmax[2999], len(nmax))
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.