Python 常見數值操作
阿新 • • 發佈:2018-11-12
取整
9//30
Out[1]: 0
取餘數
9%30
Out[2]: 9
開平方
import math
math.sqrt(36)
Out[3]: 6.0
計算冪
import math
math.pow(6, 2)
Out[4]: 36.0
取絕對值
abs()是python3 內建函式
a = -2
b = -3
c = abs(a + b)
print(c)
Out[5]: 5
根據兩點經緯度計算距離
# transfer the latitude and longitude to distance
def geo_distance (lng1,lat1,lng2,lat2):
lng1, lat1, lng2, lat2 = map(radians, [lng1, lat1, lng2, lat2])
dist_lon = lng2-lng1
dist_lat = lat2-lat1
a = sin(dist_lat/2)**2 + cos(lat1) * cos(lat2) * sin(dist_lon/2)**2
dis = 2*asin(sqrt(a))*6371*1000
return dis
long_dist = geo_distance(122.0, 38.0, 122.6 , 38.0)
lati_dist = geo_distance(122.0, 37.2, 122.0, 38.0)
print(long_dist, lati_dist)
# 結果為
52573.587711031105 88955.94131564711