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

Combine 2 arrays using numpy

If I have a list of points [x1, x2..xn, y1, y2..yn] how can I get [x1, y1, x2, y2..xn, yn] using numpy?

This is what I did, but idk how to continue

u = [x for idx, x in enumerate(l) if idx < len(l) / 2]
v = [x for idx, x in enumerate(l) if idx >= len(l) / 2]

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

>Solution :

Yo could use .reshape(2, -1), combined with .T for transpose and .flatten() to interpret the array as 1d again:

import numpy as np

# create example data: x1, ..., xn = [0, ..., 4] and y1, ..., yn = [5, ..., 9]
l = list(range(10)) 
# → [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 
z = np.array(l).reshape(2, -1).T.flatten()
# → array([0, 5, 1, 6, 2, 7, 3, 8, 4, 9])

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