0
import numpy as np

a1 = [1,2,3,4,5]
a2 = [6,7,8,9,10]
sc = np.empty((0, 2))
#sc = np.append(np.array([a1[0],a2[0]]))
#sc = np.append([a1[1],a2[1]])

sc = np.c_([a1],[a2])
#sc = np.array([a1[1],a2[1]])

print(sc)
print(sc[0])

I have tried to merge the two normal arrays but cannot find a way to do so. The desired result is sc[0] = [1 6] and sc[1] = [2 7], etc.. where [1 6] is treated as a single value.

I can see lots of error on trying different methods, is there any other possible method.

2 Answers 2

1

I am not 100% sure what you are trying, but this should do the trick:

import numpy as np
a1 = [1, 2, 3, 4, 5]
a2 = [6, 7, 8, 9, 10]
s = np.c_[a1, a2]
>>> s
array([[ 1,  6],
       [ 2,  7],
       [ 3,  8],
       [ 4,  9],
       [ 5, 10]])
>>> s[0]
array([1, 6])
Sign up to request clarification or add additional context in comments.

2 Comments

Why not use column_stack and avoid the transpose?
yap, you're right. Edited my answer
0

You could use zip to feed the array constructor with the values in the right shape:

import numpy as np

a1 = [1,2,3,4,5]
a2 = [6,7,8,9,10]

sc=np.array([*zip(a1,a2)])

print(sc)
[[ 1  6]
 [ 2  7]
 [ 3  8]
 [ 4  9]
 [ 5 10]]

Or you could use reshape on the concatenation of the two lists:

sc = np.reshape(a1+a2,(-1,2),order="F")

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.