[Numpy-discussion] puzzle: generate index with many ranges

Pierre GM pgmdevlist@gmail....
Fri Jan 30 13:06:26 CST 2009


On Jan 30, 2009, at 1:53 PM, Raik Gruenberg wrote:

> Pierre GM wrote:
>> On Jan 30, 2009, at 1:11 PM, Raik Gruenberg wrote:
>>
>>> Mhm, I got this far. But how do I get from here to a single index
>>> array
>>>
>>> [ 4, 5, 6, ... 10, 0, 1, 2, 3, 11, 12, 13, 14 ] ?
>>
>> np.concatenate([np.arange(aa,bb) for (aa,bb) in zip(a,b)])
>
> exactly! Now, the question was, is there a way to do this only using  
> numpy
> functions (sum, repeat, ...), that means without any python "for"  
> loop?

Can't really see it right now. Make np.arange(max(b)) and take the  
slices you need ? But you still have to look in 2 arrays to find the  
beginning and end of slices, so...


> Sorry about being so insistent on this one but, in my experience,  
> eliminating
> those for loops makes a huge difference in terms of speed. The zip  
> is probably
> also quite costly on a very large data set.

yeah, but it's in a list comprehension, which may make things a tad  
faster. If you prefer, use itertools.izip instead of zip, but I wonder  
where the advantage would be. Anyway, are you sure this particular  
part is your bottleneck ? You know the saying about premature  
optimization...


More information about the Numpy-discussion mailing list