在for循环内返回多个值。

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

Return several values inside for loop

问题

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

以下是该函数:

import random

def my_function():
    for i in range(3):
        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:

import random

def my_function():
    for i in range(3):
        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

`return`替换为`yield`以创建一个生成器

```python
import random

def my_function():
    for i in range(3):
        yield dict(x=[[random.randint(0,10)]], y=[[random.randint(0,10)]]), 0

g = my_function()
for d in g:
    print(d)

输出:

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

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

g = my_function()
print(next(g))
print(next(g))
print(next(g))
print(next(g))  # 会引发 StopIteration 异常

输出:

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

Replace return by yield to create a generator:

import random

def my_function():
    for i in range(3):
        yield dict(x=[[random.randint(0,10)]], y=[[random.randint(0,10)]]), 0

g = my_function()
for d in g:
    print(d)

Output:

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

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

g = my_function()
print(next(g))
print(next(g))
print(next(g))
print(next(g))  # Will raise a StopIteration exception

Output:

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

答案2

得分: 1

我希望这能让你更好地理解接下来逐个为你提供值如果你想获取所有的值请将你的函数包装在一个列表内

import random

def my_function():
    for i in range(3):
        yield(dict(x=[[random.randint(0,10)]], y=[[random.randint(0,10)]]), 0)
a = my_function()
print(next(a)) # 逐个获取
print(next(a))
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

import random

def my_function():
    for i in range(3):
        yield(dict(x=[[random.randint(0,10)]], y=[[random.randint(0,10)]]), 0)
a = my_function()
print(next(a)) # one by one
print(next(a))
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:

确定