Skip to content
Advertisement

Python: Expand 2D array to multiple 1D arrays

Consider the followoing example from np.meshgrid docs:

JavaScript

In my application, instead of x and y, I’ve 25 variables. To create a grid out of the 25 variables, one way would be:

JavaScript

However, the code will look ugly and not modular w.r.t. the number of variables (since each variable is hard-coded). Therefore, I am interested in something like the following:

JavaScript

However, I am guessing meshgrid(z) is not the correct call, and I should expand z to n_variables arrays. Any thoughts on how I can expand the 2D array into multiple 1D arrays?

Advertisement

Answer

this should do it.

JavaScript

the * operator before list, unpacks list elements. consider following:

JavaScript
Advertisement