Search code examples
pythonnumpynumba

Create np.array filled with zero arrays


I'm trying to initialize an "empty" array with each elements containing t_list a 8x8 np.zeros array :

t_list = np.zeros((8,8), dtype=np.float32)

I would now want to have a np.array with multiple t_list at each indexes:

result = np.array((t_list, t_list, ...., tlist))

I would like to be able to control the number of time t_list is in result.

I know that I could use list instead of arrays. The problem is, I put this in a numba njit function so I need to precise everything.

The aim is then to change each values in a double for loop.


Solution

  • The shape param of numpy.zeros can be a tuple of ints of any length, so you can create an ndarray with multiple dimensions.

    e.g.:

    n = 5 # or any other number that you want
    result = np.zeros((n,8,8), dtype=np.float32)