0

I have a list of arrays of different dimension and i want to concatenate those arrays into one single array.

suppose i have

LIST = [array([[0.786, 0.819]]), array([[0.811, 0.804]]), array([[0.821]])]

and i want to convert it in an array:

ARRAY = array([0.786, 0.819, 0.811, 0.804, 0.821])

so I would like to concatenate all the values of each arrays of my list in a single array

3 Answers 3

2

you can use numpy.hstack and no need for looping

import numpy as np

LIST = [np.array([[0.786, 0.819]]), np.array([[0.811, 0.804]]), np.array([[0.821]])]

arr = np.hstack([*LIST])
arr
array([[0.786, 0.819, 0.811, 0.804, 0.821]])
Sign up to request clarification or add additional context in comments.

Comments

0
l = [np.array([[0.786, 0.819]]), np.array([[0.811, 0.804]]), np.array([[0.821]])]
a = np.array([])
for x in l:
  a = np.concatenate((a, x[0]))
print (a)

Outuput

[0.786 0.819 0.811 0.804 0.821]

Comments

0
np.hstack([i[0] for i in LIST])

It could be np.hstack(LIST), if your arrays were vectors and not matrices, i.e. using only a single [], instead of [[]].

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.