获取函数外的局部变量值的所有数值。

huangapple go评论87阅读模式
英文:

Get all values of local variable value outside of function

问题

I need to print the value of prod ouside of this function and loop.
This variable prod has multiple values but I am getting only the last value when I print this outside of function even after marking it as global.

soup = BeautifulSoup(filehandle, "html.parser")
soup = soup.find('ul', class_='listing')
thdr = soup.find('div', class_='heading')

if (chk == "n"):

while (thdr != None):
for thdr in thdr:
    global prod
    temp = thdr.string
    prod = thdr.string
    print prod

我需要在这个函数和循环之外打印prod的值。
这个变量prod有多个值,但是即使将它标记为全局变量,在函数之外打印时,我只得到最后一个值。

英文:
      soup = BeautifulSoup(filehandle, "html.parser")
      soup = soup.find('ul', class_='listing')
      thdr = soup.find('div', class_='heading')

      if (chk == "n"):
         
      while (thdr != None):
      for thdr in thdr:
         global prod      
         temp = thdr.string
         prod = thdr.string
         print prod

I need to print the value of prod ouside of this function and loop.
This variable prod has multiple values but I am getting only the last value when I print this outside of function even after marking it as global.

答案1

得分: 0

你可以在顶部声明一个数组,并在每次迭代中附加项目。然后,一次打印所有值。

soup = BeautifulSoup(filehandle, "html.parser")
uls = soup.find('ul', class_='listing')
thdr = soup.find('div', class_='heading')
prod_items = []
if (chk == "n"):
    for item in thdr:
        prod = item.string
        prod_items.append(prod)
    print(prod_items)
英文:

You can declare an array on top and append items to it in each iteration. And then, print it once with all the values.

soup = BeautifulSoup(filehandle, "html.parser")
uls = soup.find('ul', class_='listing')
thdr = soup.find('div', class_='heading')
prod_items = []
if (chk == "n"):
    for item in thdr:
        prod = item.string
        prod_items.append(prod)
    print(prod_items)

答案2

得分: 0

你可以在你的 if 语句下创建一个 prod 列表(正如其他人所说),然后在 for 循环下不断追加它,最后在循环结束后添加一个 print 语句,以打印出整个 prod 列表

你不需要使用 global 关键字

这就是我想要表达的:

soup = BeautifulSoup(filehandle, "html.parser")
soup = soup.find('ul', class_='listing')
thdrs = soup.find_all('div', class_='heading')

if chk == "n":
    prods = []
    for thdr in thdrs:
        prod = thdr.string
        prods.append(prod)
        print(prod)
    print(prods)
英文:

you can make a list(as others are saying) of prod under your if statement then keep appending it under the for loop and finally you can simply add a print statement after the loop is ended to print out the whole list of prod

you don't need to use global keyword

This is what I'm trying to say:

soup = BeautifulSoup(filehandle, "html.parser")
soup = soup.find('ul', class_='listing')
thdrs = soup.find_all('div', class_='heading') 

if chk == "n":
    prods = [] 
    for thdr in thdrs: 
        prod = thdr.string
        prods.append(prod) 
        print(prod) 
    print(prods) 

答案3

得分: 0

I'm not quite sure what the code in your question is doing, so I'll use the sample code below to answer your question.

Solution

Using the yield keyword. It is used to create a generator function. A type of function that is memory efficient and can be used like an iterator object.

Sample Code

def foo(num):
    while num < 10:
        num += 1
        yield num

for n in foo(0):
    print(n)

Output

1
2
3
4
5
6
7
8
9
10
英文:

I'm not quite sure what the code in your question is doing, so I'll use the sample code below to answer your question.

Solution

Using yield keyword. It is used to create a generator function. A type of function that is memory efficient and can be used like an iterator object.

Sample Code

def foo(num):
    while num &lt; 10:
        num += 1
        yield num

for n in foo(0):
    print(n)

Output

1
2
3
4
5
6
7
8
9
10

huangapple
  • 本文由 发表于 2023年3月7日 15:01:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75658850.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定