简体   繁体   中英

Python - How to concatenate more than arrays lists in python?

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?

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]

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)

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