N
N
nevro2016-08-19 22:40:57
Python
nevro, 2016-08-19 22:40:57

2 examples: list generator and yield - the difference is not visible. Incorrect examples?

1. List generator:

gen_list = (x*x for x in range(3))

for i in gen_list:
    print(i)

0
1
4

2.yield:
def gen_list():
    lis = range(3)
    for i in lis:
        yield i*i

my_list = gen_list()
for i in my_gen:
    print(i)

0
1
4

PS
1. I understand the idea of ​​generators - to store only the current generated element in memory, i.e. it is impossible to refer to the previous one by index. Same with yield.
2. The list is an object. yield also turns the entire code of the function into an object (although the function is already an object ... apparently not iterable).
So I don’t understand the meaning of this bells and whistles over the list generator.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
R
Roman Kitaev, 2016-08-19
@deliro

In addition to Pavel Denisov 's answer , I'll throw in the following option:

def forever(lst):
    while True:
        for item in lst:
            yield item

It will yeld endlessly the elements of the list, if the list ends - again. You can't achieve this with a generator comprehension. In general, generator comprehension is like lambdas. Abbreviated notation for convenience, because it is much more convenient and clearer to write
Than
def str_gen(lst):
    for item in lst:
        yield str(item)

','.join(str_get(lst))

Although, the essence is the same.
But some things cannot be implemented with the usual generator comprehension.
PS The list generator creates a list. It's like this: [x**2 for x in range(5)]

L
lega, 2016-08-19
@lega

In this case, the list comprehension is essentially a special case of yield.
yield allows multiple entry and exit from a function, this allows you to do different things, such as streaming data or "coroutines" for asynchronous applications.

M
Matvey Yastremsky, 2016-08-30
@Sollar_MOD

that's right, generator functions differ in that they are more flexible. Generator expressions of the form(x**2 for x in range(5)) are just a simplified notation of a generator function for use in simple cases (not to be confused with list generators: [x**2 for x in range(5)]. The only difference is in brackets, but the meaning is completely different, because generator expressions produce values ​​on the go, the list generator creates the entire list in advance, and already then returns its values ​​as an iterable).

A
abcd0x00, 2016-08-27
@abcd0x00

Here is an example of a generator through a function

>>> def g():
...     while True:
...         yield 'a'
...         yield 'b'
...         yield 'c'
... 
>>> list(zip(g(), range(10)))
[('a', 0), ('b', 1), ('c', 2), ('a', 3), ('b', 4), ('c', 5), ('a', 6), ('b', 7), ('c', 8), ('a', 9)]
>>>

The function gives more opportunities for the formation of more flexible generators.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question