Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python; Append vector to an array

I've got

Xa = [a1,a2,a3]
Xb = [b1,b2,b3]
Xc = [c1,b2,b3]

And I want

X = [[a1,a2,a3],[b1,b2,b3],[c1,b2,b3]]

Im using numpy append, concatenate, hstack, vstack, and others functions, but them doesnt work or gives me this

X = [a1,a2,a3,b1,b2,b3,c1,b2,b3]

Also after this process I will need to append Xd, Xe, Xf and so on, so I need a way to add these vectors to the array as they come.

Any ideas on what Im doing wrong or what to do?

like image 416
Gorgorutos Avatar asked May 08 '26 12:05

Gorgorutos


1 Answers

It's pretty simple if just simple array. initialize an empty array and keep appending your arrays to it.

Xa = ['a1','a2','a3']
Xb = ['b1','b2','b3']
Xc = ['c1','b2','b3']

Empty array

resultArray = []
resultArray.append(Xa)
resultArray.append(Xb)
resultArray.append(Xc)

output:

[['a1','a2','a3'], ['b1','b2','b3'], ['c1','b2','b3']]

Hope this helps

Cheers

like image 158
igauravsehrawat Avatar answered May 11 '26 02:05

igauravsehrawat