Re: range

2018-06-25 Thread Schachner, Joseph
Re: "I know I'm going to get flak for bringing this up this old issue, but remember when you used to write a for-loop and it involved creating an actual list of N integers from 0 to N-1 in order to iterate through them? Crazy. But that has long been fixed - or so I thought. When I wrote, today:

Re: range() vs xrange() Python2|3 issues for performance

2011-08-04 Thread Steven D'Aprano
Chris Angelico wrote: > On Thu, Aug 4, 2011 at 4:01 AM, Steven D'Aprano > wrote: >> a, b = divmod(n, i) >> if b == 0: >> total += a+i >> > > Wouldn't this fail on squares? It happens to give correct results as > far as I've checked; no square up to 10,000 is called perfect, and > there are no pe

Re: range() vs xrange() Python2|3 issues for performance

2011-08-04 Thread Chris Angelico
On Thu, Aug 4, 2011 at 4:01 AM, Steven D'Aprano wrote: >        a, b = divmod(n, i) >        if b == 0: >            total += a+i > Wouldn't this fail on squares? It happens to give correct results as far as I've checked; no square up to 10,000 is called perfect, and there are no perfect squares

Re: range() vs xrange() Python2|3 issues for performance

2011-08-03 Thread Steven D'Aprano
harrismh777 wrote: > def perf(n): > sum = 0 > for i in range(1, n): > if n % i == 0: > sum += i > return sum == n A more effective way to speed that up is with a better algorithm: def isperfect(n): if n < 2: return False total = 1 for i in range(

Re: range() vs xrange() Python2|3 issues for performance

2011-08-02 Thread Chris Angelico
On Tue, Aug 2, 2011 at 3:45 PM, Steven D'Aprano wrote: > (But don't make the mistake of doing what I did, which was to attempt to > produce range(29000) in Python 2. After multiple *hours* of swapping, I > was finally able to kill the Python process and get control of my PC again. > Sigh.) >

Re: range() vs xrange() Python2|3 issues for performance

2011-08-02 Thread Steven D'Aprano
harrismh777 wrote: > The following is intended as a helpful small extension to the xrange() > range() discussion brought up this past weekend by Billy Mays... > > With Python2 you basically have two ways to get a range of numbers: > range() , which returns a list, and >xrange() , which r

Re: range() vs xrange() Python2|3 issues for performance

2011-08-02 Thread Chris Angelico
On Tue, Aug 2, 2011 at 10:05 AM, Peter Otten <[email protected]> wrote: > i/2 returns a float in Python 3; you should use i//2 for consistency. > And I forgot to make this change before doing my tests. Redoing the Python 3 ones with // quite drastically changes things! 3.2 (r32:88445, Feb 20 2011,

Re: range() vs xrange() Python2|3 issues for performance

2011-08-02 Thread Chris Angelico
On Tue, Aug 2, 2011 at 10:20 AM, Stefan Behnel wrote: > What version of Py3 were you using? If you used the latest, maybe even the > latest hg version, you will notice that that's substantially faster for > integers than, e.g. 3.1.x. > I just tried this out, using a slightly modified script but t

Re: range() vs xrange() Python2|3 issues for performance

2011-08-02 Thread Thomas Rachel
Am 02.08.2011 09:12 schrieb harrismh777: The following is intended as a helpful small extension to the xrange() range() discussion brought up this past weekend by Billy Mays... With Python2 you basically have two ways to get a range of numbers: range() , which returns a list, and xrange() , whic

Re: range() vs xrange() Python2|3 issues for performance

2011-08-02 Thread Stefan Behnel
harrismh777, 02.08.2011 09:12: With Python2 you basically have two ways to get a range of numbers: range() , which returns a list, and xrange() , which returns an iterator. With Python3 you must use range(), which produces an iterator; while xrange() does not exist at all (at least not on 3.2).

Re: range() vs xrange() Python2|3 issues for performance

2011-08-02 Thread Peter Otten
harrismh777 wrote: > The following is intended as a helpful small extension to the xrange() > range() discussion brought up this past weekend by Billy Mays... > > With Python2 you basically have two ways to get a range of numbers: > range() , which returns a list, and >xrange() , which r

Re: range() vs xrange() Python2|3 issues for performance

2011-08-02 Thread garabik-news-2005-05
harrismh777 wrote: these will run on either Python2 or > Python3... except that if you substitute xrange() for range() for > Python2 they will throw an exception on Python3... doh. if 'xrange' not in dir(__builtins__): xrange = range at the beginning of your program will fix that. --

Re: range with variable leading zeros

2008-05-09 Thread yhvh
> > x = [0010, 0210] > > You do realize that this is octal, right? It's unfortunate I choose that, the numbers go beyond octal > len is undefined for integers.  Perhaps you meant "len(str(x[1]))". Yes sorry it was late at night :P > You can, however, do this: > >>> '%0*d' % (5, 123) > '00123' T

Re: range with variable leading zeros

2008-05-08 Thread alex23
On May 9, 1:42 pm, yhvh <[EMAIL PROTECTED]> wrote: > I want to generate a range with variable leading zeros > > x = [0010, 0210] > padding = len(x[1]) > > for j in range(x[0], x[1]): > print (url).join('%0(pad)d(jj)'+'.jpg') %{'pad':padding, 'jj':j} > > This is syntactically incorrect, you can'

Re: range with variable leading zeros

2008-05-08 Thread Dan Bishop
On May 8, 10:42 pm, yhvh <[EMAIL PROTECTED]> wrote: > I want to generate a range with variable leading zeros > > x = [0010, 0210] You do realize that this is octal, right? > padding = len(x[1]) len is undefined for integers. Perhaps you meant "len(str(x[1]))". > for j in range(x[0], x[1]): >  

Re: range syntax

2006-11-12 Thread Cameron Laird
In article <[EMAIL PROTECTED]>, Colin J. Williams <[EMAIL PROTECTED]> wrote: . . . >Your point about iterators is well taken, but it seems that the range is >used sufficiently frequently that some syntactic form would be helpf

Re: range syntax

2006-11-11 Thread Colin J. Williams
Fredrik Lundh wrote: > Colin J. Williams wrote: > >> One of the little irritants of Python is that the range syntax is rather >> long-winded: >> [Dbg]>>> range(3, 20, 6) >> [3, 9, 15] >> [Dbg]>>> >> It would be nice if one could have something like 3:20:6. > > if you find yourself using range a

Re: range syntax

2006-11-10 Thread Antoon Pardon
On 2006-11-10, Roberto Bonvallet <[EMAIL PROTECTED]> wrote: > Colin J. Williams wrote: >> One of the little irritants of Python is that the range syntax is rather >> long-winded: >> [Dbg]>>> range(3, 20, 6) >> [3, 9, 15] >> [Dbg]>>> >> It would be nice if one could have something like 3:20:6. > >

Re: range syntax

2006-11-10 Thread Fredrik Lundh
Colin J. Williams wrote: > One of the little irritants of Python is that the range syntax is rather > long-winded: > [Dbg]>>> range(3, 20, 6) > [3, 9, 15] > [Dbg]>>> > It would be nice if one could have something like 3:20:6. if you find yourself using range a lot, maybe you should check if you

Re: range syntax

2006-11-10 Thread Roberto Bonvallet
Colin J. Williams wrote: > One of the little irritants of Python is that the range syntax is rather > long-winded: > [Dbg]>>> range(3, 20, 6) > [3, 9, 15] > [Dbg]>>> > It would be nice if one could have something like 3:20:6. In that case, how would the parser know which colon terminates the 'for

Re: range of int() type.

2006-08-23 Thread Felipe Almeida Lessa
23 Aug 2006 17:28:48 -0700, KraftDiner <[EMAIL PROTECTED]>: > This is obvious... but how do I crop off the high order bits if > necessary? > a[0]&0x ? min(a[0], 0x) ? -- Felipe. -- http://mail.python.org/mailman/listinfo/python-list

Re: range of int() type.

2006-08-23 Thread KraftDiner
Terry Reedy wrote: > "KraftDiner" <[EMAIL PROTECTED]> wrote in message > news:[EMAIL PROTECTED] > > > > So what type / class should one use to represent a 16 bit integers > > (signed or unsigned)? > > For most purposes, Python just has integers, with 'small' ones (depending > on the machine) handl

Re: range of int() type.

2006-08-23 Thread Terry Reedy
"KraftDiner" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > So what type / class should one use to represent a 16 bit integers > (signed or unsigned)? For most purposes, Python just has integers, with 'small' ones (depending on the machine) handled more efficiently. For specia

Re: range of int() type.

2006-08-23 Thread Gabriel Genellina
At Wednesday 23/8/2006 18:53, KraftDiner wrote: > |>> import sys > |>> sys.maxint > 2147483647 > So what type / class should one use to represent a 16 bit integers (signed or unsigned)? Plain int. If you need the overflow at 32K/64K, try: x & 0x Gabriel Genellina Softlab SRL

Re: range of int() type.

2006-08-23 Thread KraftDiner
Simon Forman wrote: > KraftDiner wrote: > > What is the range of a variable of type int() > > > > eg: > > i = int() > > print i.max() # 0x > > print i.min() # 0x > > > > is it a signed 16 bit or 32 bit or is it unsigned 16 or 32... > > I've noticed that it can be incremented into

Re: range of int() type.

2006-08-23 Thread Simon Forman
KraftDiner wrote: > What is the range of a variable of type int() > > eg: > i = int() > print i.max() # 0x > print i.min() # 0x > > is it a signed 16 bit or 32 bit or is it unsigned 16 or 32... > I've noticed that it can be incremented into a new class of type > long... |>> import

Re: range() is not the best way to check range?

2006-07-25 Thread Antoon Pardon
On 2006-07-23, Paul Boddie <[EMAIL PROTECTED]> wrote: > Antoon Pardon wrote: >> >> Except that if you write your own class from scratch, you can't use >> it as a slice. > > Correct, but we were actually discussing subclassing built-in classes > for use as a replacement for range/xrange. :-) I thin

Re: range() is not the best way to check range?

2006-07-23 Thread Paul Boddie
Antoon Pardon wrote: > > Except that if you write your own class from scratch, you can't use > it as a slice. Correct, but we were actually discussing subclassing built-in classes for use as a replacement for range/xrange. :-) It may be "hard work" writing all those methods in a totally new range

Re: range() is not the best way to check range?

2006-07-23 Thread Antoon Pardon
On 2006-07-21, Paul Boddie <[EMAIL PROTECTED]> wrote: > Regardless of whether myslice inherits from object or not, there's no > persuading the interpreter that it is a genuine slice, and remember > that we can't subclass slice (for some reason unknown). So, it would > appear that the interpreter re

Re: range() is not the best way to check range?

2006-07-21 Thread Steve Holden
Paul Boddie wrote: [...] > Regardless of whether myslice inherits from object or not, there's no > persuading the interpreter that it is a genuine slice, and remember > that we can't subclass slice (for some reason unknown). So, it would > appear that the interpreter really wants instances from som

Re: range() is not the best way to check range?

2006-07-21 Thread Paul Boddie
Antoon Pardon wrote: > [Subclasses of list or slice for ranges] > Except that if you write your own class from scratch, you can't use > it as a slice. For a language that is supposed to be about duck typing > I find it strange that if I make my own class with a start, stop and > step attribute, t

Re: range() is not the best way to check range?

2006-07-21 Thread Antoon Pardon
On 2006-07-20, Paul Boddie <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: >> Paul Boddie <[EMAIL PROTECTED]> wrote: >> > >> > Well, range is a function in the current implementation, although its >> > usage is similar to that one would get if it were a class, particularly >> > a subclass of list

Re: range() is not the best way to check range?

2006-07-20 Thread Alex Martelli
Paul Boddie <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > Paul Boddie <[EMAIL PROTECTED]> wrote: > > > > > > Well, range is a function in the current implementation, although its > > > usage is similar to that one would get if it were a class, particularly > > > a subclass of list or one p

Re: range() is not the best way to check range?

2006-07-20 Thread Paul Boddie
Alex Martelli wrote: > Paul Boddie <[EMAIL PROTECTED]> wrote: > > > > Well, range is a function in the current implementation, although its > > usage is similar to that one would get if it were a class, particularly > > a subclass of list or one providing a list-style interface. With such a > > cla

Re: range() is not the best way to check range?

2006-07-20 Thread Alex Martelli
Paul Boddie <[EMAIL PROTECTED]> wrote: > John Machin wrote: > > > > range() and xrange() are functions. You are suggesting that 2 > > *functions* should acquire a __contains__ method each? I trust not. > > Well, range is a function in the current implementation, although its > usage is similar to

Re: range() is not the best way to check range?

2006-07-19 Thread Dan Bishop
Paul Boddie wrote: > John Machin wrote: > > On 19/07/2006 1:05 AM, Dan Bishop wrote: > > > > > > xrange already has __contains__. > > > > As pointed out previously, xrange is a function and one would not expect > > it to have a __contains__ method. > > Well, you pointed out that range is a function

Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
tac-tics wrote: > Simon Forman wrote: > > To me, and perhaps others, "T = > > set(xrange(0, 1, 23))" and "n in T" are somewhat easier to read > > and write than "not n % 23 and 0 <= n < 1", YMMV. > > Eh? How is the first easier to read than the second?? You have a nested > function call in

Re: range() is not the best way to check range?

2006-07-18 Thread Paul Boddie
John Machin wrote: > On 19/07/2006 1:05 AM, Dan Bishop wrote: > > > > xrange already has __contains__. > > As pointed out previously, xrange is a function and one would not expect > it to have a __contains__ method. Well, you pointed out that range is a function, but xrange seems to be a type...

Re: range() is not the best way to check range?

2006-07-18 Thread John Machin
On 19/07/2006 1:05 AM, Dan Bishop wrote: > Paul Boddie wrote: > >> Yes, he wants range to return an iterator, just like xrange more or >> less does now. Given that xrange objects support __getitem__, unlike a >> lot of other iterators (and, of course, generators), adding >> __contains__ wouldn't b

Re: range() is not the best way to check range?

2006-07-18 Thread tac-tics
Simon Forman wrote: > To me, and perhaps others, "T = > set(xrange(0, 1, 23))" and "n in T" are somewhat easier to read > and write than "not n % 23 and 0 <= n < 1", YMMV. Eh? How is the first easier to read than the second?? You have a nested function call in the first! Regardless, test

Re: range() is not the best way to check range?

2006-07-18 Thread Summercoolness
[EMAIL PROTECTED] wrote: > it seems that range() can be really slow: > > if i in range (0, 1): My original use was like this: if i in range (iStart, iEnd): listData.append(a) in which iStart is 1000 and iEnd is 1008 so in that case, the program ran fine... but later on, i

Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
K.S.Sreeram wrote: > Simon Forman wrote: > > Nick Craig-Wood wrote: > >> Sets are pretty fast too, and have the advantage of flexibility in > >> that you can put any numbers in you like > >> > > > > I know this is self-evident to most of the people reading this, but I > > thought it worth pointing

Re: range() is not the best way to check range?

2006-07-18 Thread bearophileHUGS
Dan Bishop: > xrange already has __contains__. The problem is, it's implemented by a > highly-inefficient sequential search. Why not modify it to merely > check the bounds and (value - start) % step == 0? I think this is a nice idea. Bye, bearophile -- http://mail.python.org/mailman/listinfo/

Re: range() is not the best way to check range?

2006-07-18 Thread Grant Edwards
On 2006-07-18, Paul Boddie <[EMAIL PROTECTED]> wrote: >> It's unclear what you're referring to as "the range". > > The notion of something describing a range of values which can be > expanded to a list or, of relevance here, whose boundaries can be > tested efficiently. > >> Perhaps you're thinkin

Re: range() is not the best way to check range?

2006-07-18 Thread Grant Edwards
On 2006-07-18, Steve Holden <[EMAIL PROTECTED]> wrote: >> That said, "for pete's sake" is probably a just an cleaned up >> version of "for god's sake", so I probably did call pete god. > > No, actually you called god pete ;-) Well that's certainly wrong, because we all know god's name is Howard.

Re: range() is not the best way to check range?

2006-07-18 Thread Paul Boddie
Grant Edwards wrote: > > It's unclear what you're referring to as "the range". The notion of something describing a range of values which can be expanded to a list or, of relevance here, whose boundaries can be tested efficiently. > Perhaps you're thinking of a slice? Somethign like > > if (0

Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
Diez B. Roggisch wrote: > Simon Forman wrote: > > > Nick Craig-Wood wrote: > >> > >> Sets are pretty fast too, and have the advantage of flexibility in > >> that you can put any numbers in you like > >> > > > > I know this is self-evident to most of the people reading this, but I > > thought it wor

Re: range() is not the best way to check range?

2006-07-18 Thread K.S.Sreeram
Simon Forman wrote: > Nick Craig-Wood wrote: >> Sets are pretty fast too, and have the advantage of flexibility in >> that you can put any numbers in you like >> > > I know this is self-evident to most of the people reading this, but I > thought it worth pointing out that this is a great way to te

Re: range() is not the best way to check range?

2006-07-18 Thread Diez B. Roggisch
Simon Forman wrote: > Nick Craig-Wood wrote: >> >> Sets are pretty fast too, and have the advantage of flexibility in >> that you can put any numbers in you like >> > > I know this is self-evident to most of the people reading this, but I > thought it worth pointing out that this is a great way t

Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
Nick Craig-Wood wrote: > > Sets are pretty fast too, and have the advantage of flexibility in > that you can put any numbers in you like > I know this is self-evident to most of the people reading this, but I thought it worth pointing out that this is a great way to test membership in range(lo, hi

Re: range() is not the best way to check range?

2006-07-18 Thread Simon Forman
Dan Bishop wrote: > [EMAIL PROTECTED] wrote: > > it seems that range() can be really slow: > ... > > if i in range (0, 1): > > This creates a 10,000-element list and sequentially searches it. Of > course that's gonna be slow. And you're doing it 3 times. -- http://mail.python.org/m

Re: range() is not the best way to check range?

2006-07-18 Thread Steve Holden
Grant Edwards wrote: > On 2006-07-18, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > >>In <[EMAIL PROTECTED]>, tac-tics >>wrote: >> >> >>>Grant Edwards wrote: >>> for pete's sake use the comparison operator like god intended. if 0 <= i <= 1: >>> >>>I'm assuming you used

Re: range() is not the best way to check range?

2006-07-18 Thread Grant Edwards
On 2006-07-18, Paul Boddie <[EMAIL PROTECTED]> wrote: > John Machin wrote: >> >> range() and xrange() are functions. You are suggesting that 2 >> *functions* should acquire a __contains__ method each? I trust >> not. > > Well, range is a function in the current implementation, > although its usage

Re: range() is not the best way to check range?

2006-07-18 Thread Grant Edwards
On 2006-07-18, Marc 'BlackJack' Rintsch <[EMAIL PROTECTED]> wrote: > In <[EMAIL PROTECTED]>, tac-tics > wrote: > >> Grant Edwards wrote: >>> for pete's sake use the comparison operator like god intended. >>> >>> if 0 <= i <= 1: >> >> I'm assuming you used Python's compound comparison as op

Re: range() is not the best way to check range?

2006-07-18 Thread Grant Edwards
On 2006-07-18, tac-tics <[EMAIL PROTECTED]> wrote: > Grant Edwards wrote: >> for pete's sake use the comparison operator like god intended. >> >> if 0 <= i <= 1: > > I'm assuming you used Python's compound comparison as opposed to the > C-style of and'ing two comparisons together to emphasi

Re: range() is not the best way to check range?

2006-07-18 Thread Dan Bishop
Paul Boddie wrote: > Yes, he wants range to return an iterator, just like xrange more or > less does now. Given that xrange objects support __getitem__, unlike a > lot of other iterators (and, of course, generators), adding > __contains__ wouldn't be much of a hardship. Certainly, compared to > ot

Re: range() is not the best way to check range?

2006-07-18 Thread Stefan Behnel
Andy Dingley wrote: > The purpose of range() in Python is as loop control, No, the purpose of range() is to create a list, as the docs state. http://docs.python.org/lib/built-in-funcs.html """ range(...) - This is a versatile function to create lists containing arithmetic progressions. """ Stef

Re: range() is not the best way to check range?

2006-07-18 Thread Paul Boddie
John Machin wrote: > > range() and xrange() are functions. You are suggesting that 2 > *functions* should acquire a __contains__ method each? I trust not. Well, range is a function in the current implementation, although its usage is similar to that one would get if it were a class, particularly a

Re: range() is not the best way to check range?

2006-07-18 Thread John Machin
On 18/07/2006 7:22 PM, Paul Boddie wrote: > John Machin wrote: >> On 18/07/2006 12:41 PM, [EMAIL PROTECTED] wrote: >>> it seems that range() can be really slow: > > [...] > >> Some things to try: >> 1a. Read what the manual has to say about the range() function ... what >> does it produce? > > I

Re: range() is not the best way to check range?

2006-07-18 Thread Leif K-Brooks
Grant Edwards wrote: > Using xrange as somebody else suggested is also insane. Sorry about that, I somehow got the misguided notion that xrange defines its own __contains__, so that it would be about the same speed as using comparison operators directly. I figured the OP might have a better rea

Re: range() is not the best way to check range?

2006-07-18 Thread Andy Dingley
[EMAIL PROTECTED] wrote: > it seems that range() can be really slow: > if i in range (0, 1): RTFM on range() You're completely mis-using it here, using it with an if ... in ... test. The purpose of range() in Python is as loop control, not comparisons! It's not a SQL BETWEEN statement

Re: range() is not the best way to check range?

2006-07-18 Thread Paul Boddie
John Machin wrote: > On 18/07/2006 12:41 PM, [EMAIL PROTECTED] wrote: > > it seems that range() can be really slow: [...] > Some things to try: > 1a. Read what the manual has to say about the range() function ... what > does it produce? Indeed. Still, the addition of a __contains__ method to ran

Re: range() is not the best way to check range?

2006-07-18 Thread Nick Craig-Wood
Grant Edwards <[EMAIL PROTECTED]> wrote: > Creating and then searching a 10,000 element list to see if a > number is between two other numbers is insane. > Using xrange as somebody else suggested is also insane. Aye to both > If you want to know if a number is between two other numders, > f

Re: range() is not the best way to check range?

2006-07-18 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, tac-tics wrote: > Grant Edwards wrote: >> for pete's sake use the comparison operator like god intended. >> >> if 0 <= i <= 1: > > I'm assuming you used Python's compound comparison as opposed to the > C-style of and'ing two comparisons together to emphasize the fa

Re: range() is not the best way to check range?

2006-07-17 Thread tac-tics
Grant Edwards wrote: > for pete's sake use the comparison operator like god intended. > > if 0 <= i <= 1: I'm assuming you used Python's compound comparison as opposed to the C-style of and'ing two comparisons together to emphasize the fact it is god's chosen way of doing this ;-) -- htt

Re: range() is not the best way to check range?

2006-07-17 Thread Grant Edwards
On 2006-07-18, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > it seems that range() can be really slow: > > the following program will run, and the last line shows how long it ran > for: > > import time > > startTime = time.time() > > a = 1.0 > for i in range(0, 3): > if i in range (0, 100

Re: range() is not the best way to check range?

2006-07-17 Thread K.S.Sreeram
[EMAIL PROTECTED] wrote: > so if i change the line > if i in range (0, 1): > to > if i >= 0 and i < 1: [snip;] > is there an alternative use of range() or something similar that can > be as fast? you've found that alternative yourself! just use the comparison operators... in fact

Re: range() is not the best way to check range?

2006-07-17 Thread John Machin
On 18/07/2006 12:41 PM, [EMAIL PROTECTED] wrote: > it seems that range() can be really slow: > > the following program will run, and the last line shows how long it ran > for: > > import time > > startTime = time.time() > > a = 1.0 > for i in range(0, 3): > if i in range (0, 1): >

Re: range() is not the best way to check range?

2006-07-17 Thread Dan Bishop
Leif K-Brooks wrote: > [EMAIL PROTECTED] wrote: > > or is there an alternative use of range() or something similar that can > > be as fast? > > You could use xrange: > > [EMAIL PROTECTED]:~$ python -m timeit -n1 "1 in range(1)" > 1 loops, best of 3: 260 usec per loop > [EMAIL PROTECTED]

Re: range() is not the best way to check range?

2006-07-17 Thread Leif K-Brooks
[EMAIL PROTECTED] wrote: > or is there an alternative use of range() or something similar that can > be as fast? You could use xrange: [EMAIL PROTECTED]:~$ python -m timeit -n1 "1 in range(1)" 1 loops, best of 3: 260 usec per loop [EMAIL PROTECTED]:~$ python -m timeit -n1 "1 in xr

Re: range() is not the best way to check range?

2006-07-17 Thread Dan Bishop
[EMAIL PROTECTED] wrote: > it seems that range() can be really slow: ... > if i in range (0, 1): This creates a 10,000-element list and sequentially searches it. Of course that's gonna be slow. -- http://mail.python.org/mailman/listinfo/python-list

Re: Range function

2005-05-15 Thread David Formosa (aka ? the Platypus)
On 15 May 2005 02:50:38 -0700, Xah Lee <[EMAIL PROTECTED]> wrote: > Here's the Perl code. Where did you learn to program? Its highly unlikely that a Perl programer would ever write a range function as there is a built in Perl function that does the same thing. If your intent is purely accedemic

Re: Range function

2005-05-15 Thread Peter Hansen
Xah Lee wrote: > the previous posted solutions are badly botched. > def Range(iMin, iMax=None, iStep=None): [snip hideous code] > # Thanks to Peter Hansen for a correction. Ohmigod, he's only made it worse and he's blaming me for it. Shows what I get for replying to a cross-posted troll messag

Re: Range function

2005-05-15 Thread Xah Lee
the previous posted solutions are badly botched. Here's a better solution. Any further correction will appear on the website instead. (http://xahlee.org/tree/tree.html) Similar change needs to be made for the Perl code... Java code will come tomorror. By the way, the code from me are not expecte

Re: Range function

2005-05-15 Thread Peter Hansen
Xah Lee wrote: > Here's the Python solution. > # implementation note: When iStep is a decimal, rounding error > # accumulates. For example, the last item returned from > # Range(0,18,0.3) is 17.7 not 18. A remedy is to turn iStep into a > # fraction and do exact arithmetics, and possibly convert th

Re: Range function

2005-05-15 Thread Xah Lee
Here's the Python solution. -- # -*- coding: utf-8 -*- # Python # http://xahlee.org/tree/tree.html # Xah Lee, 2005-05 # implementation note: When iStep is a decimal, rounding error # accumulates. For example, the last item returned from # Range(0,18,0.3) is 17.7 not 18. A remedy is to tu

Re: Range function

2005-05-15 Thread Xah Lee
Here's the Perl code. -- #! perl # http://xahlee.org/tree/tree.html # Xah Lee, 2005-05 #_ Range _ _ _ _ =pod B Range($iMax) generates the list [1, 2, ... , $iMax]. Range($iMin, $iMax) generates the list [$iMin, ... , $iMax]. Range($iMin, $iMax, $iStep) uses incr