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

标签 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.000000,14.000000,47.206
49.000000,24.366667,31.979

必须保持高程,并将其粘贴到给定的 2D 坐标。

我尝试了简单的降序第一列和升序第二列,但没有得到所需的顺序。

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

提前致谢。

最佳答案

您也可以一次性进行排序:

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

关于python:对csv文件中的坐标进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21193823/

相关文章:

Python:用空行对文件进行排序会导致错误

python - 如何在 Python 中检测或防止同一模块的多个实例?

javascript - 按降序按两个值对数组中的对象进行排序

python - 插入排序的伪代码是否正确?

c++ - 在 C++ 中对对象数组进行冒泡排序

java - 为什么使用 Scanner.next() 会导致这种奇怪的 System.out.println() 行为?

python pandas多索引数据框选择

python - Django `bulk_create` 及相关对象

python - python应用程序的自动部署工具

python - flask-sqlalchemy 表编辑未显示