0

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]

1 Answer 1

2

Try dummy = [(np.linspace(1, i, len(nmax))) for i in nmax], what you are actually doing is dummy[2999] = np.linspace(1, nmax[nmax[2999]], len(nmax)) instead of dummy[2999] = np.linspace(1, nmax[2999], len(nmax)).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.