Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how can I merge a numpy array with a loop

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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]])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading