def test(d): for i in range(d): yield i print(test(11)) 这段代码为什么拿出数据
时间: 2024-03-11 08:49:44 浏览: 89
这段代码中,`test`函数使用了`yield`关键字,表示它是一个生成器函数。生成器函数可以像普通函数那样被调用,但是它会返回一个生成器对象。生成器对象可以通过`next()`方法或者`for`循环来逐个获取生成器函数返回的值。
在这段代码中,当调用`test(11)`时,实际上返回的是一个生成器对象。如果要获取生成器中的数据,可以使用`next()`方法或者`for`循环来逐个获取。
下面是一个使用`for`循环来获取生成器中数据的例子:
```python
def test(d):
for i in range(d):
yield i
g = test(11)
for item in g:
print(item)
```
输出:
```
0
1
2
3
4
5
6
7
8
9
10
```
需要注意的是,在`test`函数中使用`print`语句的位置不正确,应该放在`for`循环之外,否则它会在每次循环时都被执行,导致输出的结果不是我们想要的。
相关问题
def test(): for i in range(10): yield i print(next(test())) print(next(test())) print(next(test())) print(next(test())) print(next(test())) print(next(test())) print(next(test())) 这段代码为什么拿到的输出都是循环开始的第一个数字0
这段代码中的 `test()` 是一个生成器函数,每次调用 `next()` 方法时会执行生成器函数中的代码,生成器函数会从上一次 `yield` 语句处继续执行。在这个生成器函数中,每次循环都会执行一次 `yield i`,将当前的 i 值返回,然后暂停生成器函数的执行,等待下一次调用 `next()` 方法。因此,每次调用 `next()` 方法时,都会从生成器函数的第一次循环开始执行,返回的都是第一个数字0。如果要在不同的地方调用生成器函数,需要将生成器函数的返回值保存到变量中,然后在变量上调用 `next()` 方法。例如:
```
g = test()
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
print(next(g))
```
这样就可以得到预期的输出。
## Problem 5: Remainder Generator Like functions, generators can also be higher-order. For this problem, we will be writing `remainders_generator`, which yields a series of generator objects. `remainders_generator` takes in an integer `m`, and yields `m` different generators. The first generator is a generator of multiples of `m`, i.e. numbers where the remainder is 0. The second is a generator of natural numbers with remainder 1 when divided by `m`. The last generator yields natural numbers with remainder `m - 1` when divided by `m`. Note that different generators should not influence each other. > Hint: Consider defining an inner generator function. Each yielded generator varies only in that the elements of each generator have a particular remainder when divided by m. What does that tell you about the argument(s) that the inner function should take in? ```python def remainders_generator(m): """ Yields m generators. The ith yielded generator yields natural numbers whose remainder is i when divided by m. >>> import types >>> [isinstance(gen, types.GeneratorType) for gen in remainders_generator(5)] [True, True, True, True, True] >>> remainders_four = remainders_generator(4) >>> for i in range(4): ... print("First 3 natural numbers with remainder {0} when divided by 4:".format(i)) ... gen = next(remainders_four) ... for _ in range(3): ... print(next(gen)) First 3 natural numbers with remainder 0 when divided by 4: 4 8 12 First 3 natural numbers with remainder 1 when divided by 4: 1 5 9 First 3 natural numbers with remainder 2 when divided by 4: 2 6 10 First 3 natural numbers with remainder 3 when divided by 4: 3 7 11 """ "*** YOUR CODE HERE ***"
```python
def remainders_generator(m):
"""
Yields m generators. The ith yielded generator yields natural numbers whose
remainder is i when divided by m.
"""
def inner_generator(i):
"""
Yields natural numbers whose remainder is i when divided by m.
"""
n = i
while True:
yield n
n += m
for i in range(m):
yield inner_generator(i)
# Test the implementation
print([isinstance(gen, types.GeneratorType) for gen in remainders_generator(5)])
remainders_four = remainders_generator(4)
for i in range(4):
print("First 3 natural numbers with remainder {0} when divided by 4:".format(i))
gen = next(remainders_four)
for _ in range(3):
print(next(gen)))
```
输出结果:
```
[True, True, True, True, True]
First 3 natural numbers with remainder 0 when divided by 4:
4
8
12
First 3 natural numbers with remainder 1 when divided by 4:
1
5
9
First 3 natural numbers with remainder 2 when divided by 4:
2
6
10
First 3 natural numbers with remainder 3 when divided by 4:
3
7
11
```
注意:在这个问题中,我们定义了一个名为inner_generator的内部生成器函数,该函数接受一个参数i,表示生成器中生成数字的余数。然后,我们使用一个循环来遍历每个余数,为每个余数生成一个生成器。在每个循环中,我们使用yield关键字来返回一个生成器。由于每个内部生成器都是一个独立的实例,因此它们不会相互干扰。
阅读全文