Q:

Put multiple 2d numpy arrays into 3d numpy array

I'm trying to put multiple 2-D numpy arrays into one 3-D numpy array and then save the 3-D numpy array as a compressed file to a directory for later use.
I have a list that I'm looping through which will compute forecasts for different hazards. A forecast for each hazard (a 129x185 numpy array) will be computed one at a time. I want to then put each forecast array into an empty 129x185x7 numpy array.
hazlist = ['allsvr', 'torn', 'sigtorn', 'hail', 'sighail', 'wind', 'sigwind']

# Create 3-D empty numpy array
grid = np.zeros(shape=(129,185,7))

for i,haz in enumerate(hazlist):
    *do some computation to create forecast array for current hazard*
    # Now have 2-D 129x185 forecast array
    print fcst

    # Place 2-D array into empty 3-D array.
    *Not sure how to do this...*

# Save 3-D array to .npz file in directory when all 7 hazard forecasts are done.
np.savez_compressed('pathtodir/3dnumpyarray.npz')

But, I want to give each forecast array it's own grid name inside the 3-D array so that if I want a certain one (like tornadoes) I can just call it with:
filename = np.load('pathtodir/3dnumpyarray.npz')
arr = filename['torn']

It would be greatly appreciated if someone were able to assist me. Thanks.

A:

It sounds like you actually want to use a dictionary. Each dictionary entry could be a 2D array with the reference name as the key:
hazlist = ['allsvr', 'torn', 'sigtorn', 'hail', 'sighail', 'wind', 'sigwind']

# Create empty dictionary
grid = {}

for i,haz in enumerate(hazlist):
    *do some computation to create forecast array for current hazard*
    # Now have 2-D 129x185 forecast array
    print fcst

    # Place 2-D array into dictionary.
    grid[haz] = fcst  # Assuming fcst is the 2D array?

# Save 3-D array to npz file
np.savez_compressed("output", grid)

It might be best to save this as a JSON file. If the data needs to be compressed you can refer to this question and answer as to saving json in gzipped format, or this one may be clearer.
It's not clear from your example, but my assumption in the above code is that fcst is the 2D array that corresponds to the label haz in each iteration of the loop.

