简体   繁体   English

如何从for循环返回每个值

[英]How to return each value from a for loop

(Using python 3.3.2) Hi, I'm trying to make a crawling function for a text cloud, which would go into a list of links and ideally return a list of the function's output for each element in that list. (使用python 3.3.2)您好,我正在尝试为文本云创建爬网函数,该函数将进入链接列表,并理想地返回该列表中每个元素的函数输出列表。 However, I'm stuck using a print function, print(b), instead of actually returning what I want. 但是,我坚持使用打印函数print(b),而不是实际返回我想要的东西。 In my for loop, how would I return everything I would get from my print(b) statement. 在我的for循环中,我如何返回从print(b)语句获得的所有内容。 It can all be in one list or compiled some way or another. 它可以全部放在一个列表中,也可以某种方式编译。 Thank you :) tl;dr: how do I return all the stuff I get from a for loop 谢谢:) tl; dr:如何返回从for循环中获得的所有内容

def crawl():
    linkList = inputFunction()[1:][0] #makes a list of a bunch of URL's
    for i in range(len(linkList)):
        print(i)
        t = getHTML(linkList[i]) #getHTML returns tuple of text in the input URL
        alreadyCrawl = alreadyCrawl + list(linkList[i]) #ignore this
        t = list(t)
        b = counting(t) #makes dictionary of word counts
        print(b) 
    return

Either you put them in a list and return the list at the end, or you "yield" them (hence creating a generator). 您可以将它们放入列表中并在末尾返回列表,也可以“屈服”它们(因此创建了生成器)。

First way: 第一种方式:

def f():
    acc = []
    for x in range(10):
        acc.append(someFunctionOfX(x))
    return acc

Second way: 第二种方式:

def g():
    for x in range(10):
       yield someFunctionOfX(x)

Maybe the most important difference is the following: If any call to someFunctionOfX causes an exception in example 1, the function won't return anything. 也许最重要的区别如下:如果对someFunctionOfX任何调用导致示例1中的异常,则该函数将不返回任何内容。 In example 2 if let's say the 5th value cannot be yielded for some reason, the previous four have already been yielded and probably used in the caller's context. 在示例2中,如果说由于某种原因无法产生第5个值,则前四个已经产生并且可能在调用者的上下文中使用。

Here you can see the difference: 在这里您可以看到不同之处:

def f():
    acc = []
    for x in range(-3, 4):
        acc.append (2 / x)
    return acc

def g():
    for x in range(-3, 4):
        yield 2 / x

def testF():
    for x in f(): print(x)

def testG():
    for x in g(): print(x)

Calling testF simply fails (ZeroDivisionError: division by zero) and doesn't print anything. 调用testF只会失败(ZeroDivisionError:被零除),并且不会输出任何内容。 Calling testG prints 调用testG打印

-0.6666666666666666
-1.0
-2.0

and fails then (ZeroDivisionError: division by zero). 然后失败(ZeroDivisionError:被零除)。


My (very personal) criterion for either returning a list or yielding values is the following: If I need the data stored somewhere, I return a list. 我返回列表或产生值的(非常个人的)标准如下:如果我需要将数据存储在某个地方,则返回列表。 If I just need to process each member, I yield them. 如果我只需要处理每个成员,我就会产生它们。

You can return list of values that you want. 您可以返回所需的值列表。

def crawl():
    list_ret = []   #create empty list to store values

    for i in range(len(linkList)):
        # do some stuff
        b = counting(t) #makes dictionary of word counts
        list_ret.append(b)  #append value to list
        print(b) 
    return list_ret   #return list of values
def crawl():
    linkList = inputFunction()[1:][0] #makes a list of a bunch of URL's
    return_list = []
    for i in range(len(linkList)):
        print(i)
        t = getHTML(linkList[i]) #getHTML returns tuple of text in the input URL
        alreadyCrawl = alreadyCrawl + list(linkList[i]) #ignore this
        t = list(t)
        b = counting(t) #makes dictionary of word counts
        return_list.append(b) 
    return return_list

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

相关问题 从线程的每个循环获取返回值 - Get return value from each loop of thread 如何在循环中返回每个值? - How do i return each value in a loop? 如何将循环的每次迭代中的值返回到其中运行的函数? - How do I return the value from each iteration of a loop to the function it is running within? 如何使用循环在数据框中逐行读取并返回每个值或弹出每个值 - how to read row by row in a dataframe using a loop and return each value or pop out each value 如何遍历列表数组,检查每个列表中的元素并与值进行比较,以及是否与值匹配,则返回键 - How to loop through an array of lists, check an element within each list and compare to a value and if it matches the value return the key 如何从循环中获取每个 tkinter 条目的唯一值 - How to get each tkinter Entry's unique value from loop 如何按python中的每个值循环 - how to loop by each value in python 如何从Python 3中的无限循环线程获取实时返回值 - how to get a realtime return value from an infinite loop thread in Python 3 我如何使for循环从函数返回值 - How do i make the for loop return the value from a function 如何在使用格式时从我的 for 循环中返回每个值 - How to return every value from my for loop while using formatting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM