Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
append list to empty array
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)

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to append multiple <class 'str'> into a single List ahmedwaqas92 2 489 Jan-07-2021, 08:17 AM
Last Post: ahmedwaqas92
  What is the value after JOINING an empty list? JaneTan 2 406 Jan-04-2021, 06:25 PM
Last Post: deanhystad
  LIST or ARRAY Comparison and change of value nio74maz 0 272 Dec-21-2020, 05:52 PM
Last Post: nio74maz
  2d Array adds last element to entire list waiteup 2 407 Nov-19-2020, 08:25 PM
Last Post: bowlofred
  Printing empty list? hhydration 2 361 Oct-28-2020, 11:34 AM
Last Post: Atekka
  Append 2d empty lists NMMST 2 404 Oct-19-2020, 09:25 PM
Last Post: NMMST
  trouble with list array Milfredo 2 543 Sep-16-2020, 12:07 AM
Last Post: Milfredo
  How to append to list a function output? rama27 5 838 Aug-24-2020, 10:53 AM
Last Post: DeaD_EyE
  Stop a function if the list it needs is empty Pedroski55 2 537 Jul-25-2020, 11:50 PM
Last Post: Pedroski55
  Append list into list within a for loop rama27 2 722 Jul-21-2020, 04:49 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020