简体   繁体   English

与python在一行中具有多个参数的for循环

[英]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.我正在使用 google_streetview.api,但遇到了无法解决的问题。 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.我有一个带有 x 和 y 坐标的数据框,可以循环遍历。 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 ;我最终得到了很多单个 json 文件,我需要能够告诉它添加 ; for each x and y in the dataframe.对于数据框中的每个 x 和 y。

';'.join([r.POINT_X + ',' + r.POINT_Y for _, r in df.iterrows()])

Naturally, you need to specify you're adding the x and y points to the location index of the params dictionary.当然,您需要指定将 x 和 y 点添加到params字典的location索引。

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.请注意,我假设 params 已经存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM