yield tuple
时间: 2023-08-31 09:09:54 浏览: 130
A yield tuple is a sequence of values that are returned by a generator function using the yield keyword. The values in the tuple can be of any data type and can be returned one at a time, or all at once as a single tuple. Here is an example of a generator function that yields a tuple:
```
def generate_tuples():
for i in range(5):
yield (i, i * 2)
for tup in generate_tuples():
print(tup)
```
Output:
```
(0, 0)
(1, 2)
(2, 4)
(3, 6)
(4, 8)
```
In this example, the generator function generates tuples containing two values - the first value is the loop variable `i`, and the second value is `i` multiplied by 2. The tuples are returned one at a time using the `yield` keyword, and are printed in a loop.
阅读全文
相关推荐















