我一直在得到奇怪的结果,我终于注意到我在元组中放置空格的习惯导致了这个问题。如果你能重现这个问题并告诉我它为什么会这样,你就可以拯救我剩下的头发了。谢谢!
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
元组中有空格,答案是错误的。没有,答案是正确的。
最佳答案
空格应该没有区别。区别在于大小写:SW
与 Sw
。
你不检查这里的SW
:
compass in ['S', 'W', 'Sw']]
也许改成这样:
compass.upper() in ['S', 'W', 'SW']]
关于python - 为什么 Python 元组中的空格很重要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7548562/