简体   繁体   English

如何将循环的每次迭代中的值返回到其中运行的函数?

[英]How do I return the value from each iteration of a loop to the function it is running within?

I'm making a program that scrapes local bus times from a real time information server and prints them. 我正在编写一个程序,该程序从实时信息服务器中抓取本地总线时间并进行打印。 In order to return all the bus times, it does this: 为了返回所有的公交时间,它这样做:

while i < len(info["results"]):
    print "Route Number:" + " " + info['results'][i]['route']
    print "Due in" + " " + info["results"][i]["duetime"] + " " + "minutes." + "\n"
    i = i + 1

This works fine, and returns all of the results, one by one like so: 这样可以正常工作,并像下面这样一个接一个地返回所有结果:

Route Number: 83 Due in 12 minutes. 航线号:83在12分钟内交。

Route Number: 83 Due in 25 minutes. 航线号:83在25分钟内交。

Route Number: 83A Due in 39 minutes. 路线号码:83A在39分钟内到达。

Route Number: 83 Due in 55 minutes. 路线号码:83在55分钟内交。

However, as I'm using this feature within another script, I turned the code to fetch times and return them into a function: 但是,当我在另一个脚本中使用此功能时,我将代码转换为获取时间并将其返回到函数中:

def fetchtime(stopnum):
    data = "?stopid={}".format(stopnum)+"&format=json"
    content = "https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation"
    req = urllib2.urlopen(content + data + "?")

    i = 0 
    info = json.load(req)

    if len(info["results"]) == 0:
        return "Sorry, there's no real time info for this stop!"

    while i < len(info["results"]):
        return "Route Number:" + " " + str(info['results'][i]['route']) + "\n" + "Due in" + " " + str(info["results"][i]["duetime"]) + " " + "minutes." + "\n"
        i = i + 1 

This works, however it only returns the first bus from the list given by the server, instead of however many buses there may be. 此方法有效,但是它仅返回服务器给出的列表中的第一条总线,而不是可能有多少条总线。 How do I get the printed result of the function to return the info supplied in each iteration of the loop? 如何获得函数的打印结果以返回循环的每次迭代中提供的信息?

Can you not just make a list and return the list? 您不仅可以列出并返回列表吗?

businfo = list()
while i < len(info["results"]):
    businfo.append("Route Number:" + " " + str(info['results'][i]['route']) + "\n" + "Due in" + " " + str(info["results"][i]["duetime"]) + " " + "minutes." + "\n")
    i = i + 1 

return businfo

You will have to edit the printing commands that this function returns to. 您将必须编辑此功能返回的打印命令。

I would suggest you to use the yield statement instead return in fetchtime function. 我建议您使用yield语句代替在fetchtime函数中返回。

Something like: 就像是:

def fetchtime(stopnum):
    data = "?stopid={}".format(stopnum)+"&format=json"
    content = "https://data.dublinked.ie/cgi-bin/rtpi/realtimebusinformation"
    req = urllib2.urlopen(content + data + "?")

    i = 0 
    info = json.load(req)

    if len(info["results"]) == 0:
        yield "Sorry, there's no real time info for this stop!"

    while i < len(info["results"]):
        yield "Route Number:" + " " + str(info['results'][i]['route']) + "\n" + "Due in" + " " + str(info["results"][i]["duetime"]) + " " + "minutes." + "\n"
        i = i + 1 

It would allow you to pick one data at a time and proceed. 它将允许您一次选择一个数据并继续。

Lets say that info["results"] is a list of length 2, then you could do: 假设info [“ results”]是长度为2的列表,那么您可以这样做:

>> a = fetchtime(data)
>> next(a)
Route Number: 83 Due in 25 minutes.
>> next(a) 
Route Number: 42 Due in 33 minutes.
>> next(a)
StopIteration Error
or simple do:
>> for each in a:
    print(each)
Route Number: 83 Due in 25 minutes.
Route Number: 42 Due in 33 minutes.
# In case if there would be no results (list would be empty), iterating 
# over "a" would result in:
>> for each in a:
    print(each)
Sorry, there's no real time info for this stop!

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

相关问题 如何在 function 内的循环内返回值? - How do I return a value within a loop within a function? 如何在循环中返回每个值? - How do i return each value in a loop? 如何在 for 循环中按列对数组进行切片,为每次迭代将数组中的新列添加到新数组中? - How do I slice an array column-wise within a for loop, adding a new column from the array to a new array for each iteration? 我如何使for循环从函数返回值 - How do i make the for loop return the value from a function 如何使用Python创建一个函数,该函数将在数据表中的每一行的字典中返回一个值? - How do I create a function that will return a value in a dictionary for each row within a data sheet using Python? 我如何在while循环中返回值 - How do I return a value within a while loop 您如何使用范围 function 以在 for 循环中的每次迭代以 1 为增量从 0 计数到 8 - How do you use the range function to count from 0 to 8 by increments of 1 for each iteration in a for loop 如何将for循环的每次迭代的结果存储在变量中 - How do I store the results of each iteration of a for loop inside a variable 如何在 Python 中打印出 for 循环的每次迭代? - How do I print out each iteration of the for loop in Python? 如何从循环中打印或返回迭代的最终值? - How to print or return the final value of the iteration from loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM