I have a generator object returned by multiple yield. Preparation to call this generator is rather time-consuming operation. That is why I want to reuse the generator several times.
y = FunctionWithYield() for x in y: print(x) #here must be something to reset "y" for x in y: print(x)
Of course, I"m taking in mind copying content into simple list. Is there a way to reset my generator?
Generators can"t be rewound. You have the following options:
Run the generator function again, restarting the generation:
y = FunctionWithYield() for x in y: print(x) y = FunctionWithYield() for x in y: print(x)
Store the generator results in a data structure on memory or disk which you can iterate over again:
y = list(FunctionWithYield()) for x in y: print(x) # can iterate again: for x in y: print(x)
The downside of option 1 is that it computes the values again. If that"s CPU-intensive you end up calculating twice. On the other hand, the downside of 2 is the storage. The entire list of values will be stored on memory. If there are too many values, that can be unpractical.
So you have the classic memory vs. processing tradeoff. I can"t imagine a way of rewinding the generator without either storing the values or calculating them again.
Another option is to use the
itertools.tee() function to create a second version of your generator:
import itertools y = FunctionWithYield() y, y_backup = itertools.tee(y) for x in y: print(x) for x in y_backup: print(x)
This could be beneficial from memory usage point of view if the original iteration might not process all the items.
Roger Jennings is an author and consultant specializing in Microsoft .NET n-tier database applications and data-intensive Windows Communication Foundation (WCF) Web services with SQL Server. He’s be...
A Problem-Solver’s Guide to Building Real-World Intelligent Systems. Data is the new oil and Machine Learning is a powerful concept and framework for making the best out of it. In this age of aut...
Why this Book? Hadoop has been the base for most of the emerging technologies in today’s big data world. It changed the face of distributed processing by using commodity hardware for large data set...
The field of Artificial Intelligence (AI), which can definitely be considered to be the parent field of deep learning, has a rich history going back to 1950. While we will not cover this history in mu...