从给定输入中提取纬度和经度

时间:2014-03-11 03:08:22

标签: python

我想从给定的输入中提取纬度和经度

input=-6.00753745047,53.4988487471 -6.00649712382,53.4977981241 -6.00788849553,53.4962929888 -6.00660721705,53.4967836409

这是我尝试但没有输出的代码

for pos in range(len(input)):
 lt,lo=input[pos].split(',')
  print "lat is:",lt , "and long is:",lo

1 个答案:

答案 0 :(得分:0)

如果input真的是长字符串:

input = "-6.00753745047,53.4988487471 -6.00649712382,53.4977981241 " + \
        "-6.00788849553,53.4962929888 -6.00660721705,53.4967836409"

for lt, lo in (pair.split(',') for pair in input.split()):
    print "lat is:", lt, "and long is:", lo

输出:

lat is: -6.00753745047 and long is: 53.4988487471
lat is: -6.00649712382 and long is: 53.4977981241
lat is: -6.00788849553 and long is: 53.4962929888
lat is: -6.00660721705 and long is: 53.4967836409

ltlo将使用此字符串值。如果您希望数值使用instread进行计算,可以使用相同的for循环进行转换:

for lt, lo in (map(float, pair.split(',')) for pair in input.split()):
    ...