我在CSV文件中有两列,我想用Python创建一个循环,所以我可以匹配一个列表中的每个元素到另一个列表。我这样做是为了创建一个包含来自GoogleMaps API的多个行程的文件。所以我真正想要做的是将两个列表中的每个元素的起始列表中的距离映射到目的地列表。
它是这样的:
origins = ['a', 'b', 'c', 'd']
destinations = ['E', 'F', 'G']
我想要一个像这样的输出:
Distances_from = ['aE', 'aF', 'aG', 'bE', 'bF', 'bG', 'cE', 'cF', 'cG', 'dE', 'dF', 'dG']
我的代码是:
destination_index=0 #city code
destination_coordinates=10
origin_index=12
origin_coordinates=13
reader=csv.reader(municipalities)
writer=csv.writer(output)
try:
for row in list(reader)[1:]:
destination_c=row[destination_coordinates]
origin_c=row[origin_coordinates]
destination_name=row[destination_index]
origin_name=row[origin_index]
for destination_coordinates, origin_coordinates in zip(destination_c, origin_c):
distances = gmaps.distance_matrix(origin_c, destination_c)
if distances['rows'][0]['elements'][0]['status'] == "OK":
writer.writerow((destination_name,origin_name,
distances['rows'][0]['elements'][0]['distance']['value'],
distances['rows'][0]['elements'][0]['duration']['value']))
else:
writer.writerow((destination_name,origin_name,
".",
"."))
finally:
municipalities.close()
output.close()
答案 0 :(得分:0)
列表理解将会:
origins=['a','b','c','d']
destinations=['E','F','G']
Distances_from = ["{}{}".format(x,y) for x in origins for y in destinations]
print(Distances_from)
# ['aE', 'aF', 'aG', 'bE', 'bF', 'bG', 'cE', 'cF', 'cG', 'dE', 'dF', 'dG']
显然,你需要改变你想做的事情(可能不是"{}{}".format(x,y)
)。
答案 1 :(得分:0)
它是否与嵌套for循环一样简单,您正在搜索:
for origin in origins:
for destination in destinations:
distance(origin, destination)
...
要以更简洁的方式写出来,你可以这样做:
from functools import product
for origin, destination in product(origins, destinations):
distance(origin, destination)
或使用列表理解:
distances = [distance(origin, destination)
for origin in origins
for destination in destinations]
答案 2 :(得分:0)
我想我几乎已经解决了,我一直有一些问题,但我认为这是因为Google API而不是代码。 这是我使用的代码。
@JanHak
dest_f=open(os.path.join(dir,'Colombian_Municipalities.csv'),'r')
orig_f=open(os.path.join(dir,'Origins.csv'),'r')
output_f=open(os.path.join(dir,'Distances_to_Bogota.csv'),'w')
# Constants
dest_code_index=0 # detination city code
dest_coord_index=10 # Coordinates from all municipalities in Colombia
orig_code_index=0 #
orig_coord_index=1 # Coordinates from markets and seaports
dest_reader=csv.reader(dest_f)
orig_reader=csv.reader(orig_f)
output_writer=csv.writer(output_f)
output_header=['Origin','Destination','Distance (meters)','Duration (seconds)']
output_writer.writerow(output_header)
orig_reader_list=list(orig_reader)
dest_reader_list=list(dest_reader)
try:
for dest_row in dest_reader_list[1:]:
dest_code=dest_row[dest_code_index]
dest_coord=dest_row[dest_coord_index]
for orig_row in orig_reader_list[1:]:
orig_code=orig_row[orig_code_index]
orig_coord=orig_row[orig_coord_index]
print(orig_code,dest_code)
distances=gmaps.distance_matrix(orig_coord, dest_coord)
if distances['rows'][0]['elements'][0]['status']=="OK":
output_writer.writerow((dest_code,orig_code,
distances['rows'][0]['elements'][0]['distance']['value'],
distances['rows'][0]['elements'][0]['duration']['value']))
else:
output_writer.writerow((dest_code,orig_code,
".",
"."))
finally:
dest_f.close()
orig_f.close()
output_f.close()