0

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) i.e

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?

1
  • Why do you want to avoid lists? Commented Jan 31, 2021 at 21:16

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.