简体   繁体   中英

Appending numpy array of arrays

I am trying to append an array to another array but its appending them as if it was just one array. What I would like to have is have each array appended on its own index, (withoug having to use a list, i want to use np arrays) ie

temp = np.array([])
for i in my_items
   m = get_item_ids(i.color)  #returns an array as [1,4,20,5,3]  (always same number of items but diff ids
   temp = np.append(temp, m, axis=0)

On the second iteration lets suppose i get [5,4,15,3,10]

then i would like to have temp as array([1,4,20,5,3][5,4,15,3,10]) But instead i keep getting [1,4,20,5,3,5,4,15,3,10]

I am new to python but i am sure there is probably a way to concatenate in this way with numpy without using lists?

You have to reshape m in order to have two dimension with

m.reshape(-1, 1)

thus adding the second dimension. Then you could concatenate along axis=1.

np.concatenate(temp, m, axis=1)

List append is much better - faster and easier to use correctly.

temp = []
for i in my_items
    m = get_item_ids(i.color)  #returns an array as [1,4,20,5,3]  (always same number of items but diff ids
    temp = m

Look at the list to see what it created. Then make an array from that:

 arr = np.array(temp)
 # or `np.vstack(temp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM