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

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

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.

  1. soup = BeautifulSoup(filehandle, "html.parser")
  2. soup = soup.find('ul', class_='listing')
  3. thdr = soup.find('div', class_='heading')
  4. if (chk == "n"):
  5. while (thdr != None):
  6. for thdr in thdr:
  7. global prod
  8. temp = thdr.string
  9. prod = thdr.string
  10. print prod

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

英文:
  1. soup = BeautifulSoup(filehandle, "html.parser")
  2. soup = soup.find('ul', class_='listing')
  3. thdr = soup.find('div', class_='heading')
  4. if (chk == "n"):
  5. while (thdr != None):
  6. for thdr in thdr:
  7. global prod
  8. temp = thdr.string
  9. prod = thdr.string
  10. 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

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

  1. soup = BeautifulSoup(filehandle, "html.parser")
  2. uls = soup.find('ul', class_='listing')
  3. thdr = soup.find('div', class_='heading')
  4. prod_items = []
  5. if (chk == "n"):
  6. for item in thdr:
  7. prod = item.string
  8. prod_items.append(prod)
  9. 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.

  1. soup = BeautifulSoup(filehandle, "html.parser")
  2. uls = soup.find('ul', class_='listing')
  3. thdr = soup.find('div', class_='heading')
  4. prod_items = []
  5. if (chk == "n"):
  6. for item in thdr:
  7. prod = item.string
  8. prod_items.append(prod)
  9. print(prod_items)

答案2

得分: 0

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

你不需要使用 global 关键字

这就是我想要表达的:

  1. soup = BeautifulSoup(filehandle, "html.parser")
  2. soup = soup.find('ul', class_='listing')
  3. thdrs = soup.find_all('div', class_='heading')
  4. if chk == "n":
  5. prods = []
  6. for thdr in thdrs:
  7. prod = thdr.string
  8. prods.append(prod)
  9. print(prod)
  10. 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:

  1. soup = BeautifulSoup(filehandle, "html.parser")
  2. soup = soup.find('ul', class_='listing')
  3. thdrs = soup.find_all('div', class_='heading')
  4. if chk == "n":
  5. prods = []
  6. for thdr in thdrs:
  7. prod = thdr.string
  8. prods.append(prod)
  9. print(prod)
  10. 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

  1. def foo(num):
  2. while num < 10:
  3. num += 1
  4. yield num
  5. for n in foo(0):
  6. print(n)

Output

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 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

  1. def foo(num):
  2. while num &lt; 10:
  3. num += 1
  4. yield num
  5. for n in foo(0):
  6. print(n)

Output

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 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:

确定