在for循环内返回多个值。

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

Return several values inside for loop

问题

我有一个必须使用for循环返回多个值的函数。我不希望将这些值存储在列表或字典中。由于使用return,我只得到第一个值。如何连续返回所有值?我尝试使用生成器和yield,但不确定如何使用它。

以下是该函数:

  1. import random
  2. def my_function():
  3. for i in range(3):
  4. return(dict(x=[[random.randint(0,10)]], y=[[random.randint(0,10)]]), 0)

生成器和使用yield适用于我的需求吗?

英文:

I have a function that must return several values using a for loop. I do not wish to store the values inside a list or a dict. Because of the use of the return, I only get the first value. How can I return all values successively? I tried using generators and yield but I'm not sure how to use it.

here is the function:

  1. import random
  2. def my_function():
  3. for i in range(3):
  4. return(dict(x=[[random.randint(0,10)]], y=[[random.randint(0,10)]]), 0)

Are generators and the use of yield suited for my need?

答案1

得分: 1

  1. `return`替换为`yield`以创建一个生成器
  2. ```python
  3. import random
  4. def my_function():
  5. for i in range(3):
  6. yield dict(x=[[random.randint(0,10)]], y=[[random.randint(0,10)]]), 0
  7. g = my_function()
  8. for d in g:
  9. print(d)

输出:

  1. ({'x': [[0]], 'y': [[10]]}, 0)
  2. ({'x': [[0]], 'y': [[1]]}, 0)
  3. ({'x': [[3]], 'y': [[0]]}, 0)

您还可以使用next手动消耗下一个值:

  1. g = my_function()
  2. print(next(g))
  3. print(next(g))
  4. print(next(g))
  5. print(next(g)) # 会引发 StopIteration 异常

输出:

  1. ({'x': [[4]], 'y': [[4]]}, 0)
  2. ({'x': [[4]], 'y': [[9]]}, 0)
  3. ({'x': [[7]], 'y': [[2]]}, 0)
  4. ...
  5. StopIteration:
英文:

Replace return by yield to create a generator:

  1. import random
  2. def my_function():
  3. for i in range(3):
  4. yield dict(x=[[random.randint(0,10)]], y=[[random.randint(0,10)]]), 0
  5. g = my_function()
  6. for d in g:
  7. print(d)

Output:

  1. ({'x': [[0]], 'y': [[10]]}, 0)
  2. ({'x': [[0]], 'y': [[1]]}, 0)
  3. ({'x': [[3]], 'y': [[0]]}, 0)

You can also use next to consume manually the next value:

  1. g = my_function()
  2. print(next(g))
  3. print(next(g))
  4. print(next(g))
  5. print(next(g)) # Will raise a StopIteration exception

Output:

  1. ({'x': [[4]], 'y': [[4]]}, 0)
  2. ({'x': [[4]], 'y': [[9]]}, 0)
  3. ({'x': [[7]], 'y': [[2]]}, 0)
  4. ...
  5. StopIteration:

答案2

得分: 1

  1. 我希望这能让你更好地理解接下来逐个为你提供值如果你想获取所有的值请将你的函数包装在一个列表内
  2. import random
  3. def my_function():
  4. for i in range(3):
  5. yield(dict(x=[[random.randint(0,10)]], y=[[random.randint(0,10)]]), 0)
  6. a = my_function()
  7. print(next(a)) # 逐个获取
  8. print(next(a))
  9. print(list(my_function())) # 获取所有值
英文:

I hope, this gives you better understanding. next gives you value one by one and if you want all values wrap your function inside a list

  1. import random
  2. def my_function():
  3. for i in range(3):
  4. yield(dict(x=[[random.randint(0,10)]], y=[[random.randint(0,10)]]), 0)
  5. a = my_function()
  6. print(next(a)) # one by one
  7. print(next(a))
  8. print(list(my_function())) # get all values

huangapple
  • 本文由 发表于 2023年2月14日 19:14:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75447018.html
匿名

发表评论

匿名网友

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

确定