-1

I am trying to fulfill an array with the data from other two variables

my code is like:

import numpy as np

x1 = [200,50,125,275,350]
x2 = [475,575,700,700,575]

#array = np.array([[200,475],[50,575],[125,700],[275,700],[350,575]])
array = np.array(
                    for j in range(5):
                      #for i in range(2):
                        [x1[i],x2[i]]
                    )

thank you in advance

Expected Result array([[200,475], [50,575], [125,700], [275,700], [350,575]])

1
  • Please show the expected result. Commented Aug 23, 2022 at 18:32

3 Answers 3

1

You can use zip then convert to list and use numpy.asarray to get as array Or you can use numpy.column_stack.

import numpy as np

x1 = [200,50,125,275,350]
x2 = [475,575,700,700,575]

res = np.asarray(list(zip(x1,x2)))

res_2 = np.column_stack((x1,x2))

>>> res
array([[200, 475],
       [ 50, 575],
       [125, 700],
       [275, 700],
       [350, 575]])

>>> res_2
array([[200, 475],
       [ 50, 575],
       [125, 700],
       [275, 700],
       [350, 575]])
Sign up to request clarification or add additional context in comments.

2 Comments

Thnak you very much I share with you the whole code: colab.research.google.com/drive/…
@DanielMolina, OK, I see this, What do you want?
1

You can do it by concatenation, but you can also do it just by building a 2D array and transposing:

import numpy as np
x1 = [200,50,125,275,350]
x2 = [475,575,700,700,575]

a = np.array([x1,x2]).T
print(a)

Output:

[[200 475]
 [ 50 575]
 [125 700]
 [275 700]
 [350 575]]

2 Comments

Thank you very much for your time, I share with you the complete code: colab.research.google.com/drive/…
ZIp also works for this. Whichever makes more sense to you.
1

You can use a built in function of numpy - numpy.stack()

import numpy as np

x1 = [200,50,125,275,350]
x2 = [475,575,700,700,575]

arr = np.stack((x1, x2), axis=1)
print(arr)

Output:

[[200 475]
 [ 50 575]
 [125 700]
 [275 700]
 [350 575]]

1 Comment

Thnak you very much: I share the whole code: colab.research.google.com/drive/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.