append list to empty array - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: append list to empty array (/thread-24149.html) |
append list to empty array - SchroedingersLion - Feb-02-2020 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. RE: append list to empty array - SchroedingersLion - Feb-02-2020 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) |