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]
CodePudding user response:
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])
CodePudding user response:
Numpy solution (using np.column_stack()
instead of zip
):
list_a = np.array([100.0, 200.0, -10.0])
list_b = [False, False, True]
print(np.column_stack((list_a, list_b)))
[[100. 0.]
[200. 0.]
[-10. 1.]]
Alternative:
list_a = [100.0, 200.0, -10.0]
list_b = [False, False, True]
newlist = []
for val_a, val_b in zip(list_a, list_b):
newlist.append((val_a, val_b))
print(newlist)
[(100.0, False), (200.0, False), (-10.0, True)]
Further information: Link