1 | initial version |
Hi Tlk!
Generators come from python, and I found a similar question at stackoverflow:
http://stackoverflow.com/questions/1271320/reseting-generator-object-in-python
The basic answer seems to be that generators can't be reset. Instead, you could re-run the code which creates the generator to make a fresh copy of it, or you could store the outputs of the generator in a list so that you can reuse them. If neither of these will work, you could perhaps modify how the generator is built so that re-creating it does not take too long (for example, do computationally intense parts first, store the output, and use that to create the generator).
Feel free to post more details about your particular problem if you want :)
good luck! Niles
2 | added example |
Hi Tlk!
Generators come from python, and I found a similar question at stackoverflow:
http://stackoverflow.com/questions/1271320/reseting-generator-object-in-python
The basic answer seems to be that generators can't be reset. Instead, you could re-run the code which creates the generator to make a fresh copy of it, or you could store the outputs of the generator in a list so that you can reuse them. If neither of these will work, you could perhaps modify how the generator is built so that re-creating it does not take too long (for example, do computationally intense parts first, store the output, and use that to create the generator).
UPDATE: For the particular example you gave, it's easy enough to just recreate the generator:
sage: generator = (n for n in range(1,10000) if (is_prime(n) & is_prime(n+2)))
sage: generator.next()
3
sage: generator.next()
5
sage: generator.next()
11
sage: generator = (n for n in range(1,10000) if (is_prime(n) & is_prime(n+2)))
sage: generator.next()
3
sage: generator.next()
5
Feel free to post more details about your particular problem if you want :)
good luck! Niles