Created
June 18, 2015 10:55
-
-
Save graingerkid/6ce29be1f4bc521525af to your computer and use it in GitHub Desktop.
Helper function to split a generator into a specified size.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def last_50(generator, n): | |
''' | |
Takes a generator object and returns | |
a specified number (n) into a list. | |
''' | |
counter = 0 | |
results = [] | |
for i in generator: | |
if counter == n: | |
break | |
else: | |
results.append((i)) | |
counter += 1 | |
return results | |
def gen(): | |
''' | |
This is an example generator | |
for testing the last_50() function | |
''' | |
for i in range(10000000): | |
yield i | |
print last_50(gen(), 200) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment