Home > OS >  How do i make a list with x1,y1,x2,y2,x3,y3.... into (x1,y1), (x2,y2), (x3,y3)
How do i make a list with x1,y1,x2,y2,x3,y3.... into (x1,y1), (x2,y2), (x3,y3)

Time:04-03

the two input list are coordinates x and y. They are input separately, and I am trying to combine the two input into xy coordinates in array form.

I am trying to make two list into an array

for example here is the two individual list: x = [x1,x2,x3,x4] y = [y1,y2,y3,y4]

then I combine the input into one list:

xy=[x for y in(x, y) for x in y]

where it is arrange in [x1,y1,x2,y2,x3,y3,x4,y4]

however I'm stuck in the next step which is making it into an array that has this order

array_xy = ((x1,y1),(x2,y2),(x3,y3),(x4,y4))

Any idea how can I make the xy list into the array_xy?

CodePudding user response:

You can make it directly from x and y with zip:

list(zip(x, y))

I hope I understand what you want correctly.

By the way, I think xy is [x1, x2, x3, x4, y1, y2, y3, y4] how you wrote it.

But you can construct what you want from it like this:

array_xy = [(xy[i], xy[i len(x)]) for i in range(len(x))]
  • Related