Compressed index sets, generators and iterators

Tim Hochberg tim.hochberg at ieee.org
Wed Nov 1 21:27:22 CST 2006


A. M. Archibald wrote:
> On 01/11/06, Bill Baxter <wbaxter at gmail.com> wrote:
>
>   
>> What's the reason iterators are not supported currently?
>> For instance  A[range(0,4)] works for a 1d A, but A[xrange(0,4)] does not.
>> Are iterators just too inefficient to bother with?
>>     
>
> If you want an iterator back, a generator comprehension will do it:
> (A[i] for i in xrange(0,4))
>
> If the result is to be a numpy array, the size must be known ahead of
> time (so that the memory can be allocated) and specified. At this
> point, and considering the overhead in calling back and forth to the
> generator, the cost of converting to a list (after all,
> A[list(xrange(0,40)] should work fine) isn't necessarily worth the
> trouble.
>   
Another option for converting an iterator to a sequence that might be 
helpful, particularly if result is large, is to use fromiter. For example:

    a[numpy.fromiter(xrange(0,40, 2), int)]


-tim


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642




More information about the Numpy-discussion mailing list