3

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?

1
  • 1
    Edit your question to make it clear where you want numpy arrays, and where you are happy with lists. Regarding your use of vstack etc, show us how you used the functions and what was wrong. Commented Jul 20, 2015 at 16:44

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

You aren't using arrays here, at least not numpy ones. You using Python lists.
@hpaulj OP didn't said he had numpy array. he just mentioned he used various methods. i just took OP code and put the result as he wanted.
The OP was sending mixed signals, showing lists, but tagging the question with numpy and mentioning various numpy functions. As well as talking about arrays and vectors. But since he accepted your answer, that numpy stuff must have been a mistake.
0

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')

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.