Python Forum

Full Version: append list to empty array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello guys,

I would like to know why the following does not give me an Nx7 array.

X_y_disch = np.empty((0,7), float)
for keys in B1_B2:
    X_y_disch = np.append(X_y_disch, [[k for k in B1_B2[keys]["Discharge-Model"].values()] + [B1_B2[keys]["Cycle Life"][0,0]]])
where B1_B2 is a nested dictionary. Each k in the "k for k" loop yields a float, i.e.
[k for k in B1_B2[keys]["Discharge-Model"].values()]
is a list of N-1 floats.
B1_B2[keys]["Cycle Life"][0,0]
yields a float as well.

I obtain a 1d array, instead of N x 7.
I found the solution: One has to specify axis=0 in order to get what I want, otherwise the arrays are flattened before appending is performed.

X_y_disch = np.append(X_y_disch, [[k for k in B1_B2[keys]["Discharge-Model"].values()] + [B1_B2[keys]["Cycle Life"][0,0]]], axis=0)