Generators And For Loops In Python
So I have a generator function, that looks like this. def generator(): while True: for x in range(3): for j in range(5): yield x After I lo
Solution 1:
generator()
initializes new generator object:
In [4]: generator() is generator() # Creating 2 separate objects
Out[4]: False
Then generator().next()
gets the first value from the newly created generator object (0 in your case).
You should call generator
once:
In [5]: gen = generator() # Storing new generator object, will reuse it
In [6]: [gen.next() for _ in range(6)] # Get first 6 values for demonstration purposes
Out[6]: [0, 0, 0, 0, 0, 1]
Note: generator.next
was removed from Python 3 (PEP 3114) - use the next
function instead:
In [7]: next(gen)
Out[7]: 1
Solution 2:
With each call of generator
you are creating a new generator object:
generator().next() # 1st item in 1st generator
generator().next() # 1st item in 2nd generator
Create one generator, and then call the next
for subsequent items:
g = generator()
g.next() # 1st item in 1st generator
g.next() # 2nd item in 1st generator
Solution 3:
I posted it in the comment, I think if you look at this in parallel with the question you see the mistake:
with open('my_file.txt', 'r') as f:
print(f.readline()) # `readline` simply reads a single line
with open('my_file.txt', 'r') as f:
print(f.readline())
with open('my_file.txt', 'r') as f:
print(f.readline())
with open('my_file.txt', 'r') as f:
print(f.readline())
Instead of making a new generator object each time, you have to make it once and then use it until it has been depleted:
mygen = generator()
mygen.next()
mygen.next()
...
etc
Post a Comment for "Generators And For Loops In Python"