[英]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.