graukas
graukas

Reputation: 61

Iterate over list to perform requests

I have a list of coordinates, '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]]

For these coordinates I need to perform requests in pairs of two like that (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.

The request body looks like that:

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)

The above code will use the whole list of coordinates, as defined in "coordinates":coords - now I need to have an iteration over the body for "coordinates":pair1, "coordinates":pair2, etc.

I tried using a for and a while loop, but can't seem to define that e.g. "coordinates":coords[x,x+1]

Is there a way to do that?

Upvotes: 0

Views: 492

Answers (3)

Rudy
Rudy

Reputation: 467

Something like this should work:

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

Here we are using list comprehension to generate a list of coordinate tuples, and then we iterate over the generated tuples.

You can also use the same iteration technique in the for loop if you are not interested in generating the list of coordinate pairs at all.

Upvotes: 0

L.Grozinger
L.Grozinger

Reputation: 2398

A combination of slicing and the zip built-in function could be used to generate the coordinates to be used for each request. E.g.

coords = [[1, 2], [3, 4], [5, 6]]
coords = zip(coords, coords[1:])

Then you can loop over them with e.g.

for c1, c2 in coords:

Upvotes: 3

Amit Nanaware
Amit Nanaware

Reputation: 3346

You can use for loop over the list and create the coordinates with two values.

you can try below code:

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

Upvotes: 0

Related Questions