从列表创建 numpy 数组数组

麦克斯韦的守护进程

我想从列表中创建一个数组数组(下面代码中的 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))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章