I have set of three dimensional coordinates in csv file, like: [two first columns are respectively latitude and longitude in decimal degrees, third one is elevation]
49.000000,14.000000,47.206
55.000000,14.000000,34.727
49.000000,24.366667,31.979
55.000000,24.366667,24.744
I would like to sort the coordinates in the following order: starting from north-west corner then go east-south, like:
55.000000,14.000000,34.727
55.000000,24.366667,24.744
49.000000,14.000000,47.206
49.000000,24.366667,31.979
Elevation has to be kept, glued to given 2D coordinates.
I tried simple descending 1st column and ascending 2nd column but got no desired order.
import csv
coordinates = [];
file_in = csv.reader(open('NewFile.csv', 'rb'), delimiter=',', quotechar='|');
file_out = '11dataout.txt'
fout=open(file_out,"w")
for row in file_in:
coordinates.append(row)
coordinates.sort(key=lambda x: x[1], reverse=True)
coordinates.sort(key=lambda x: x[2])
fout.write(str(coordinates))
fout.write('\n')
Thanks in advance.
您也可以一次性进行排序:
coordinates.sort(key = lambda x: (-x[0], x[1]))
You problem is that you assume lists are 1-indexed, when they aren't:
[55.000000, 14.000000, 34.727]
#0 1 2
Also, you sort in the wrong order, you need to sort by longitude then latitude:
coordinates.sort(key=lambda x: x[1])
coordinates.sort(key=lambda x: x[0], reverse=True)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.