[Numpy-discussion] number ranges (was Re: Matlab page on scipy wiki)

Colin J. Williams cjw at sympatico.ca
Mon Feb 13 20:09:05 EST 2006

Tim Hochberg wrote:

> Bill Baxter wrote:
>> On 2/11/06, *Gary Ruben* <gruben at bigpond.net.au 
>> <mailto:gruben at bigpond.net.au>> wrote:
>>     Sasha wrote:
>>     > On 2/10/06, Gary Ruben <gruben at bigpond.net.au
>>     <mailto:gruben at bigpond.net.au>> wrote:
>>     >> ...  I must say that Travis's
>>     >> example numpy.r_[1,0,1:5,0,1] highlights my pet hate with
>>     python - that
>>     >> the upper limit on an integer range is non-inclusive.
>>     >
>>     > In this case you must hate that an integer range starts at 0 (I
>>     don't
>>     > think you would want len(range(10)) to be 11).
>> First, I think the range() function in python is ugly to begin with.  
>> Why can't python just support range notation directly like 'for a in 
>> 0:10'.  Or with 0..10 or 0...10 syntax.  That seems to make a lot 
>> more sense to me than having to call a named function.   Anyway, 
>> that's a python pet peeve, and python's probably not going to change 
>> something so fundamental...
>> Second, sometimes zero-based, non-inclusive ranges are handy, and 
>> sometimes one-based inclusive ranges are handy.  For array indexing, 
>> I personally like zero based.  But sometimes I just want a list of N 
>> numbers like a human would write it, from 1 to N, and in those cases 
>> it seems really odd for N+1 to show up.
>> This is a place where numpy could do something.  I think it would be 
>> nice if numpy had something like an 'irange' (inclusive range) 
>> function to complement the 'arange' function.  They would act pretty 
>> much the same, except irange(5) would return [1,2,3,4,5], and 
>> irange(1,5) would return [1,2,3,4,5].
>> Anyway, I think I'm going to put a little irange function in my setup. 
> FWIW, I'd recomend a different name. irange sounds like it belongs in 
> the itertools module with ifilter, islice, izip, etc. Perhaps, rangei 
> would work, although admittedly it's harder to see. Maybe crange for 
> closed range (versus half-open range)? I dunno, but irange seems like 
> it's gonna confuse someone, if not you, then other people who end up 
> looking at your code.
> -tim

Wouldn't it be nice if we could express range(a, b, c) as a:b:c?

Colin W.

More information about the NumPy-Discussion mailing list