Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python:对csv文件中的坐标进行排序_Python_Sorting_Csv_Coordinates - Fatal编程技术网

python:对csv文件中的坐标进行排序

python:对csv文件中的坐标进行排序,python,sorting,csv,coordinates,Python,Sorting,Csv,Coordinates,我在csv文件中设置了一组三维坐标,如: [前两列分别为纬度和经度(十进制度数),第三列为高程] 49.000000,14.000000,47.206 55.000000,14.000000,34.727 49.000000,24.366667,31.979 55.000000,24.366667,24.744 我想按以下顺序对坐标进行排序:从西北角开始,然后往东往南,如: 55.000000,14.000000,34.727 55.000000,24.366667,24.744 49.000

我在csv文件中设置了一组三维坐标,如: [前两列分别为纬度和经度(十进制度数),第三列为高程]

49.000000,14.000000,47.206
55.000000,14.000000,34.727
49.000000,24.366667,31.979
55.000000,24.366667,24.744
我想按以下顺序对坐标进行排序:从西北角开始,然后往东往南,如:

55.000000,14.000000,34.727
55.000000,24.366667,24.744
49.000000,14.000000,47.206
49.000000,24.366667,31.979
标高必须保持不变,并与给定的二维坐标保持一致

我尝试了简单的下降第一列和上升第二列,但没有得到想要的顺序

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')

提前感谢。

您的问题是假设列表是1索引的,而不是:

[55.000000, 14.000000, 34.727]
#0          1          2
此外,排序顺序错误,需要先按经度排序,然后按纬度排序:

coordinates.sort(key=lambda x: x[1])                              
coordinates.sort(key=lambda x: x[0], reverse=True)

您还可以一次完成排序:

coordinates.sort(key = lambda x: (-x[0], x[1]))

是的,您在索引方面是对的,但这是错误的,在我的原始文件中,前面还有一列。无论如何,按照您提出的方式对摘录的四点进行排序似乎是可以的,但在较大的文件中会出现错误的结果。