Meshgriding an array with itself n times, in Python

Viewed 55

Assume tht x is a 1 dimensional array in Python. How can we compute its n-times meshgrid with itself, without writing it n-times? For example, for n=8, I want a command to do

a1, a2, a3, a4, a5, a6, a7, a8 = numpy.meshgrid(x,x,x,x,x,x,x,x)

but without writing a1, a2, a3, a4, a5, a6, a7, a8 and x,x,x,x,x,x,x,x.

1 Answers

Just do:

mesh = np.meshgrid(*[x]*8)

and you can access mesh[0], mesh[1], etc.

Related