3

I've got 2 numpy arrays x1 and x2. Using python 3.4.3

x1 = np.array([2,4,4])
x2 = np.array([3,5,3])

I would like to a numpy array like this:

[[2,3],[4,5],[4,3]]

How would i go about this?

3 Answers 3

8

You can use numpy.column_stack:

In [40]: x1 = np.array([2,4,4])

In [41]: x2 = np.array([3,5,3])

In [42]: np.column_stack((x1, x2))
Out[42]: 
array([[2, 3],
       [4, 5],
       [4, 3]])
Sign up to request clarification or add additional context in comments.

2 Comments

Pretty nice! Numpy built-in solution. I didn't know that.
Thank you!, this is perfect
2

Yep. It sounds like zip function:

import numpy as np 

x1 = np.array([2,4,4])
x2 = np.array([3,5,3])

print zip(x1, x2) # or [list(i) for i in zip(x1, x2)]

2 Comments

The OP specifically asks for the output to be a numpy array
@mtrw Yep! You're right. The Warren's answer is the best solution.
-1

You can zip the 2 arrays like this:

x3 = list(zip(x1,x2))

Output:

[(2, 3), (4, 5), (4, 3)]

This above code creates a list of tuples. If you want a list of lists, you can use list comprehension:

x3 = [list(i) for i in list(zip(x1,x2))]

Output:

[[2, 3], [4, 5], [4, 3]]

2 Comments

The OP specifically asks for the output to be a numpy array
@mtrw This is what the OP posted as a desired output: [[2,3],[4,5],[4,3]]. Yes, I know he called it a numpy array...

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.