-2

I have two 1D array: a=[1 2 3] b=[4 5 6]

How to combine them to an array c like this? c=[[1 4] [2 5] [3 6]]

3
  • Search for "Stacking NumPy arrays". Commented Nov 8, 2016 at 13:18
  • 1
    Do you have lists or numpy arrays? Commented Nov 8, 2016 at 13:18
  • stacking or zip will return an array of list, I don't want to have a list (e.g. [(1, 4), ...] Commented Nov 8, 2016 at 13:20

3 Answers 3

1

You can do :

a = [1, 2, 3]
b = [4, 5, 6]

np.vstack((a,b)).T

Result :

array([[1, 4],
       [2, 5],
       [3, 6]])
Sign up to request clarification or add additional context in comments.

1 Comment

np.hstack gives array([1, 2, 3, 4, 5, 6])
0

You can zip them together

python2

c = zip(a,b)

python3

c = list(zip(a,b))

Both pythons if you want a list in list

c = [[i, j] for i, j in zip(a, b)]

3 Comments

zip(a,b) is already a list
True for python2, i made this clear in my solution :-)
But still, he/she wants a list of list and not a list of tuples which is the case with zip
0

You could try this:

c = [[a[i], b[i]] for i in range(len(a))]

My output:

[[1, 4], [2, 5], [3, 6]]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.