Creating two concatenated arrays from a generator

Viewed 650

Consider the following example in Python 2.7. We have an arbitrary function f() that returns two 1-dimensional numpy arrays. Note that in general f() may returns arrays of different size and that the size may depend on the input.

Now we would like to call map on f() and concatenate the results into two separate new arrays.

import numpy as np

def f(x):
    return np.arange(x),np.ones(x,dtype=int)   

inputs = np.arange(1,10)
result = map(f,inputs)
x = np.concatenate([i[0] for i in result]) 
y = np.concatenate([i[1] for i in result]) 

This gives the intended result. However, since result may take up much memory, it may be preferable to use a generator by calling imap instead of map.

from itertools import imap
result = imap(f,inputs)
x = np.concatenate([i[0] for i in result]) 
y = np.concatenate([i[1] for i in result]) 

However, this gives an error because the generator is empty at the point where we calculate y.

Is there a way to use the generator only once and still create these two concatenated arrays? I'm looking for a solution without a for loop, since it is rather inefficient to repeatedly concatenate/append arrays.

Thanks in advance.

1 Answers
Related