What is the most efficient way to get a fixed number of items from a generator?
What I currently do is using zip
and range
. In this example I take
chunks of size 3 from the generator.
def f():
x = 0
while x < 21:
yield x
x += 1
g = f()
while True:
x = [i for _, i in zip(range(3), g)]
if not x:
break
print x
The background is that the database I use provides a generator object for query results. Than I fill a fixed size numpy array with data and process it as one batch.
Use itertools.islice
:
import itertools
for elem in itertools.islice(f(), 3):
print elem
and directly into your numpy array:
my_arr = np.array(itertools.islice(f(), 3))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With