简体   繁体   中英

creation of numpy array of arrays from list

I want to create an array of arrays from a list (nmax in the code below). I'm trying to use list comprehension but I'm not getting the results I expect. If I assign the rows manually, it works as expected:

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.        ])

But with list comprehension, it gives me different results:

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.        ])

The change from one value to the other is at index 2585 and I don't know why.

This is the full code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM