遍历列表以执行请求
Iterate over list to perform requests
我有一个坐标列表,'coords':
[[14.21055347, 47.5674345], [16.39356558, 48.17711001], [14.21055347, 47.5674345], [16.29236955, 48.15006768], [16.32467573, 48.13840484], [16.147533399999997, 48.3067388], [14.502926, 48.19992600000001]]
对于这些坐标,我需要像 (x, x+1) 这样成对地执行请求:
Pair 1: [[14.21055347, 47.5674345], [16.39356558, 48.17711001]]
Pair 2: [[16.39356558, 48.17711001], [14.21055347, 47.5674345]]
Pair 3: [[14.21055347, 47.5674345], [16.29236955, 48.15006768]]
etc.
请求正文如下所示:
body = {"coordinates":coords,"extra_info":["waycategory"]}
headers = {
'Accept': 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8',
'Content-Type': 'application/geo+json; charset=utf-8'
}
call = requests.post('http://127.0.0.1:8080/ors/v2/directions/driving-hgv/geojson', json=body, headers=headers)
weiter = call.text
data = json.loads(weiter)
以上代码将使用“坐标”:coords 中定义的整个坐标列表 - 现在我需要对“坐标”:pair1、“坐标”:pair2 等主体进行迭代。
我尝试使用 for 和 while 循环,但似乎无法定义它,例如“坐标”:坐标[x,x+1]
有办法吗?
您可以使用 for 循环遍历列表并创建具有两个值的坐标。
你可以试试下面的代码:
ls = [[14.21055347, 47.5674345], [16.39356558, 48.17711001], [14.21055347, 47.5674345], [16.29236955, 48.15006768], [16.32467573, 48.13840484], [16.147533399999997, 48.3067388], [14.502926, 48.19992600000001]]
for i in range(len(ls)-1):
coords = [ls[i],ls[i+1]]
print(coords)
your next logic with coords
像这样的东西应该可以工作:
coords = [[14.21055347, 47.5674345], [16.39356558, 48.17711001], [14.21055347, 47.5674345], [16.29236955, 48.15006768], [16.32467573, 48.13840484], [16.147533399999997, 48.3067388], [14.502926, 48.19992600000001]]
# Generate a list of coordinate tuples using list comprehension
pairs = [(coords[i], coords[i+1]) for i in range(len(coords) - 1)]
for pair in pairs:
body = {"coordinates": pair, "extra_info": ["waycategory"]}
# Send the request here
这里我们使用list comprehension生成坐标元组列表,然后迭代生成的元组。
如果您对生成坐标对列表完全不感兴趣,也可以在 for 循环中使用相同的迭代技术。
切片和 zip
内置函数的组合可用于生成用于每个请求的坐标。例如
coords = [[1, 2], [3, 4], [5, 6]]
coords = zip(coords, coords[1:])
然后你可以循环遍历它们,例如
for c1, c2 in coords:
我有一个坐标列表,'coords':
[[14.21055347, 47.5674345], [16.39356558, 48.17711001], [14.21055347, 47.5674345], [16.29236955, 48.15006768], [16.32467573, 48.13840484], [16.147533399999997, 48.3067388], [14.502926, 48.19992600000001]]
对于这些坐标,我需要像 (x, x+1) 这样成对地执行请求:
Pair 1: [[14.21055347, 47.5674345], [16.39356558, 48.17711001]]
Pair 2: [[16.39356558, 48.17711001], [14.21055347, 47.5674345]]
Pair 3: [[14.21055347, 47.5674345], [16.29236955, 48.15006768]]
etc.
请求正文如下所示:
body = {"coordinates":coords,"extra_info":["waycategory"]}
headers = {
'Accept': 'application/json, application/geo+json, application/gpx+xml, img/png; charset=utf-8',
'Content-Type': 'application/geo+json; charset=utf-8'
}
call = requests.post('http://127.0.0.1:8080/ors/v2/directions/driving-hgv/geojson', json=body, headers=headers)
weiter = call.text
data = json.loads(weiter)
以上代码将使用“坐标”:coords 中定义的整个坐标列表 - 现在我需要对“坐标”:pair1、“坐标”:pair2 等主体进行迭代。
我尝试使用 for 和 while 循环,但似乎无法定义它,例如“坐标”:坐标[x,x+1]
有办法吗?
您可以使用 for 循环遍历列表并创建具有两个值的坐标。
你可以试试下面的代码:
ls = [[14.21055347, 47.5674345], [16.39356558, 48.17711001], [14.21055347, 47.5674345], [16.29236955, 48.15006768], [16.32467573, 48.13840484], [16.147533399999997, 48.3067388], [14.502926, 48.19992600000001]]
for i in range(len(ls)-1):
coords = [ls[i],ls[i+1]]
print(coords)
your next logic with coords
像这样的东西应该可以工作:
coords = [[14.21055347, 47.5674345], [16.39356558, 48.17711001], [14.21055347, 47.5674345], [16.29236955, 48.15006768], [16.32467573, 48.13840484], [16.147533399999997, 48.3067388], [14.502926, 48.19992600000001]]
# Generate a list of coordinate tuples using list comprehension
pairs = [(coords[i], coords[i+1]) for i in range(len(coords) - 1)]
for pair in pairs:
body = {"coordinates": pair, "extra_info": ["waycategory"]}
# Send the request here
这里我们使用list comprehension生成坐标元组列表,然后迭代生成的元组。
如果您对生成坐标对列表完全不感兴趣,也可以在 for 循环中使用相同的迭代技术。
切片和 zip
内置函数的组合可用于生成用于每个请求的坐标。例如
coords = [[1, 2], [3, 4], [5, 6]]
coords = zip(coords, coords[1:])
然后你可以循环遍历它们,例如
for c1, c2 in coords: