pythonfor-loopmultiple-arguments

For loop with multiple arguments in one line with python


I'm using the google_streetview.api and I have a problem I can't solve. The documentation tells me I can run multiple arguments in a single line by separating with ; but I don't know how I loop with values inside a line. I have a dataframe with x and y coordinates that I loop over. The standard version looks like this:

params = [{
'size': '600x300', # max 640x640 pixels
'location': '46.414382,10.013988',
'heading': '151.78',
'pitch': '-0.76',
'key': 'your_dev_key'
}]

And I need the line:

'location': '1234,1234',

To go like this:

for coor, row in df.iterrows():
    x=row.POINT_X
    y=row.POINT_Y
    'location': 'POINT_Y1,POINT_X1; POINT_Y2, POINT_X2; and so on',

I did the loop first for the full parameter but when I skip the separation with ; I end up with a lot of single json-files and I need to be able to tell it to add the ; for each x and y in the dataframe.


Solution

  • Naturally, you need to specify you're adding the x and y points to the location index of the params dictionary.

    You would want to build a list out of the coordinates and join them in a string:

    #creates a list of string with the (x, y) coordinates
    coords = [','.join([row.POINT_X, row.POINT_Y]) for row in df.iterrows()]
    #creates a string out of the coordinates separated by ";"
    #and sets it as the value for the location index in params.
    params[0]['location'] = ';'.join(coords)
    

    Notice that I'm assuming params already exists.