0

I have many arrays, let's say

X = np.array([1, 2, 3, 4])
Y = np.array([5, 6, 7, 8])
Z = np.array([9, 10, 11, 12])

I want to concatenate them all so I have

arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

Here is what I've tried

arr = X + Y + Z

this doesn't work, here is my result

>>> print(arr)
[15, 18, 21, 24]

Just the sum of each element at an index i. Any suggestions?

6
  • 2
    Can't reproduce, your code works for me. Also, don't call your list list, it masks the built-in type Commented Oct 12, 2018 at 3:13
  • are you sure they're lists? X + Y + Z should give you what you want. Commented Oct 12, 2018 at 3:14
  • Based on the results, you "lists" are not lists but numpy array. Please confirm. Commented Oct 12, 2018 at 3:15
  • I run it and got what your expected. Commented Oct 12, 2018 at 3:16
  • @DYZ just checked the type. You are right, it is not a list, but a numpy array. I'll edit the question now Commented Oct 12, 2018 at 3:18

2 Answers 2

1

you can use np.concatenate()

a = np.concatenate((X, Y, Z))
print(a)
[ 1  2  3  4  5  6  7  8  9 10 11 12]
Sign up to request clarification or add additional context in comments.

Comments

0

Use np.concatenate to combine np arrays:

import numpy as np 
X = np.array([1, 2, 3, 4])
Y = np.array([5, 6, 7, 8])
Z = np.array([9, 10, 11, 12])

print np.concatenate((X,Y,Z), axis=0)

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.