下面是我当前的代码。
import csv
def function():
latitude = []
longitude = []
with open('data.csv', 'rU') as input: #
dL= list(csv.reader(input))
sL = [row[4] for row in dL[1:]]
longitude.append(sL)
sL1 = [row[3] for row in dL[1:]]
latitude.append(sL1)
print latitude
print longitude
print
for i in range(0, len(latitude)):
print "Co-ordinates (" + str(latitude[i]) + "," + str(longitude[i]) + ")"
function()
是的,所以我的输出产生了这个
[['-20.71', '-20.73', '-20.88', '-20.78', '-20.76', '-20.68', '-20.95', '-21.06', '-21.05', '-20.9']]
[['116.77', '116.75', '116.67', '117.15', '117.16', '117.19', '117.37', '117.44', '116.26', '117.64']]
Co-ordinates (['-20.71', '-20.73', '-20.88', '-20.78', '-20.76', '-20.68', '-20.95', '-21.06', '-21.05', '-20.9'],['116.77', '116.75', '116.67', '117.15', '117.16', '117.19', '117.37', '117.44', '116.26', '117.64'])
这显示了我想要生成的列表。
我想要做的是产生如下所示的输出
Co-ordinates (-20.71,116.77)
Co-ordinates (-20.73, 116.75)
对于所有坐标对依此类推。
有什么方法可以获得我想要的输出坐标吗?
最佳答案
- 您想使用
extend
而不是append
。 zip(纬度,经度)
再想一想,您可以对 dL[1:]] 中的行执行longitude = [row[4] for row in dL[1:]]
。您不需要扩展(或附加或初始化)任何内容。
更好:
with open('data.csv', 'rU') as infile:
dl = list(csv.reader(infile))
lat_long = [tuple(row[3:5]) for row in dl[1:]]
for (lat, lng) in lat_long:
print "Co-ordinates ({0}, {1})".format(lat, lng)
关于python - 将列表中的值分开以形成坐标对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16892627/