1

As I did in cases zArr and rzArr I want to use function dl(z) to create an array or list dlArr that contains the generated values from dl given z from zArr and rz from rzArr. I tried doing it like this but the syntax is wrong:

dlArr= [(dl(z,rz) for z in zArr & rz in rzArr)]

Relating code for reference:

zArr = np.linspace(0.01, 2.0, 1048)

def dist_integrand(z):
   dist_integrand = 1.0 / np.sqrt(Omegam * (1 + z) ** 3 + Omegal)
   return dist_integrand

def rz(z):
   rz = integrate.quad(dist_integrand, 0, z)
   return rz

rzArr = (rz(z) for z in zArr)

def dl(z,rz):
    dl = (1+z) * rz
    return dl

#!!
dlArr= [(dl(z,rz) for z in zArr & rz in rzArr)]

1 Answer 1

1

You want to iterate over a zipped pair of arrays:

... for z, rz in zip(zArr, rzArr)

https://docs.python.org/3/library/functions.html#zip

The zip( ... ) call is generating a sequence of tuples, and then a tuple unpack operation binds the z, rz names to each generated value.


To better see the details of what's going on, try this:

from pprint import pp

pp(list(zip(zArr, rzArr)))
Sign up to request clarification or add additional context in comments.

3 Comments

arr1 & arr2 would be an interesting syntactic sugar for zip() though...
It already has a meaning: take the bitwise AND of the two arguments.
Not for lists / sequences though. Ditto how bitwise OR was added for dict merging. Not saying it's a good idea, just an interesting one from the OP :)

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.