Ron*_*nja 9 python indexing list minimum latitude-longitude
我有一长串的经度值(len(Lon)= 420481)和另一个纬度值.我想找到经度最小值的相应纬度.
我试过了:
SE_Lat = [Lat[x] for x,y in enumerate(Lon) if y == min(Lon)]
Run Code Online (Sandbox Code Playgroud)
但这需要很长时间才能完成.
有谁知道更有效的方式?
也许你也有这样的建议:我现在尝试找到与新经度最接近的相应纬度,这不在原始经度向量中.我试过这个:
minDiff = [min(abs(x - lon_new) for x in lons)] # not very quick, but works
[(lat,lon) for lat,lon in izip(lats,lons) if abs(lon-lon_new)==minDiff]
Run Code Online (Sandbox Code Playgroud)
最后一行抛出错误,因为有多个匹配.我现在不知道如何只找到一个值,让我们说第一个.任何帮助是极大的赞赏!
我可以推荐numpy吗?
import numpy
nplats = numpy.array(lats)
nplons = numpy.array(lons)
# this part is 20x faster than using the built-in python functions
index = numpy.argmin(nplats)
print nplats[index], nplons[index]
Run Code Online (Sandbox Code Playgroud)
这比min(izip())解决方案快(使用420481随机创建的记录时使用我的设置大约20倍),虽然当然你需要将数据值存储在numpy中以利用这种加速.
min(itertools.izip(Lat, Lon), key=operator.itemgetter(1))[0]
Run Code Online (Sandbox Code Playgroud)