python一次性输入3个数_在python for循环中一次运行3个变量。

1586010002-jmsa.png

For loop with multiple variables in python 2.7.

Hello,

I am not certain how to go about this, I have a function that goes to a site and downloads a .csv file. It saves the .csv file in a particular format: name_uniqueID_dataType.csv. here is the code

import requests

name = "name1"

id = "id1"

dataType = "type1"

def downloadData():

URL = "http://www.website.com/data/%s" %name #downloads the file from the website. The last part of the URL is the name

r = requests.get(URL)

with open("data/%s_%s_%s.csv" %(name, id, dataType), "wb") as code: #create the file in the format name_id_dataType

code.write(r.content)

downloadData()

The code downloads the file and saves it perfectly fine. I want to run a for loop on the function that takes those three variables each time. The variables will be written as lists.

name = ["name1", "name2"]

id = ["id1", "id2"]

dataType = ["type1", "type2"]

There will be over 100 different items listed in each list with the same amount of items in each variable. Is there any way to accomplish this using a for loop in python 2.7. I have been doing research on this for the better part of a day but I can't find a way to do it. Please note that I am new to python and this is my first question. Any assistance or guidance would be greatly appreciated.

解决方案

zip the lists and use a for loop:

def downloadData(n,i,d):

for name, id, data in zip(n,i,d):

URL = "http://www.website.com/data/{}".format(name) #downloads the file from the website. The last part of the URL is the name

r = requests.get(URL)

with open("data/{}_{}_{}.csv".format(name, id, data), "wb") as code: #create the file in the format name_id_dataType

code.write(r.content)

Then pass the lists to your function when calling:

names = ["name1", "name2"]

ids = ["id1", "id2"]

dtypes = ["type1", "type2"]

downloadData(names, ids, dtypes)

zip will group your elements by index:

In [1]: names = ["name1", "name2"]

In [2]: ids = ["id1", "id2"]

In [3]: dtypes = ["type1", "type2"]

In [4]: zip(names,ids,dtypes)

Out[4]: [('name1', 'id1', 'type1'), ('name2', 'id2', 'type2')]

So the first iteration name,id and data will be ('name1', 'id1', 'type1') and so on..

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python的for循环可用于一次性迭代列表、元组、字符串和其他序列类型的所有元素。要在一次循环获取序列的每个元素并执行某些操作,可以使用for循环。 要在一次循环一次性输出列表的所有元素,可以使用以下语法: ``` my_list = [1, 2, 3, 4, 5] for item in my_list: print(item) ``` 这将输出: ``` 1 2 3 4 5 ``` 在上面的示例,我们首先创建了一个名为`my_list`的列表,并将其设置为包含一些整数。然后,我们使用for循环迭代整个列表,并将每个元素存储在变量`item`。最后,我们打印`item`的值,这将输出列表的所有元素。 如果要一次性将元组的所有元素打印出来,可以使用类似的语法: ``` my_tuple = (1, 2, 3, 4, 5) for item in my_tuple: print(item) ``` 这将输出: ``` 1 2 3 4 5 ``` 在这个示例,我们创建了一个名为`my_tuple`的元组,并使用for循环迭代整个元组,将每个元素存储在变量`item`。最后,我们打印`item`的值,这将输出元组的所有元素。 注意,使用for循环一次性迭代一个字符串的所有字符与迭代列表或元组的方法相同。例如,以下代码将一次性打印字符串的所有字符: ``` my_string = "Hello, world!" for char in my_string: print(char) ``` 这将输出: ``` H e l l o , w o r l d ! ``` 在这个示例,我们创建了一个名为`my_string`的字符串,并使用for循环迭代整个字符串,将每个字符存储在变量`char`。最后,我们打印`char`的值,这将输出字符串的所有字符。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值