Gorgorutos
Gorgorutos

Reputation: 35

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?

Upvotes: 3

Views: 10074

Answers (2)

igauravsehrawat
igauravsehrawat

Reputation: 3954

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

Upvotes: 2

Kasravnd
Kasravnd

Reputation: 107287

You can use np.vstack :

Xa =np.array(['a1','a2','a3'])
Xb =np.array( ['b1','b2','b3'])
Xc = np.array(['c1','b2','b3'])

>>> np.vstack((Xa,Xb,Xc))
array([['a1', 'a2', 'a3'],
       ['b1', 'b2', 'b3'],
       ['c1', 'b2', 'b3']], 
      dtype='|S2')

Upvotes: 0

Related Questions