Home > Net >  How to reverse a list of latitude and longitude coordinate pairs?
How to reverse a list of latitude and longitude coordinate pairs?

Time:08-14

I have a list of tuples where each tuple contains a pair of coordinates. I would like to reverse the coordinates for each point.

I have a dataframe, and a specific column called "coord" contains a list of latitude and longitude coordinate pairs at each row, and I would like to reverse the pair of coordinates of each row.

As an example, the first row looks like this

 [(52.34725, 4.91790),
 (52.34715, 4.91797),
 (52.34742, 4.91723),
 (52.34752, 4.91713)]

I tried this function, but it does not work.

result =  [[p[1], p[0]] for p in x]

The expected output is:

[(4.91790, 52.34725),
 (4.91797, 52.34715),
 (4.91723, 52.34742),
 (4.91713, 52.34752)]

CodePudding user response:

Try:

reversed = [item[::-1] for item in x]

CodePudding user response:

y = [(xx[1],xx[0]) for xx in x]
y  # [(4.9179, 52.34725), (4.91797, 52.34715), (4.91723, 52.34742), (4.91713, 52.34752)]

CodePudding user response:

Another possible solution:

y = []
for i, j in x:
  y.append((j, i))

Output:

[(4.9179, 52.34725), (4.91797, 52.34715), (4.91723, 52.34742), (4.91713, 52.34752)]
  • Related