Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get a fixed number of items from a generator [duplicate]

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.

like image 759
tauran Avatar asked Apr 24 '13 09:04

tauran


1 Answers

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))
like image 192
eumiro Avatar answered Sep 22 '22 02:09

eumiro