我得到了奇怪的结果,我终于注意到我在元组中放置空格的习惯导致了这个问题。如果你能重现这个问题并告诉我为什么它会这样工作,你就会节省我剩下的头发。谢谢!
jcomeau@intrepid:/tmp$ cat haversine.py
#!/usr/bin/python
def dms_to_float(degrees):
d, m, s, compass = degrees
d, m, s = int(d), float(m), float(s)
float_degrees = d + (m / 60) + (s / 3600)
float_degrees *= [1, -1][compass in ['S', 'W', 'Sw']]
return float_degrees
jcomeau@intrepid:/tmp$ python
Python 2.6.7 (r267:88850, Jun 13 2011, 22:03:32)
[GCC 4.6.1 20110608 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from haversine import *
>>> dms_to_float((111, 41, 0, 'SW'))
111.68333333333334
>>> dms_to_float((111,41,0,'Sw'))
-111.68333333333334
元组中有空格,答案是错误的。没有,答案是正确的。