Re: python loops

2006-09-08 Thread Jay
Here are my benchmarks for those interested in version 2.4.3 [EMAIL PROTECTED]:~$ python -V Python 2.4.3 [EMAIL PROTECTED]:~$ python -mtimeit 'for x in range(1000):pass' 1 loops, best of 3: 64.2 usec per loop [EMAIL PROTECTED]:~$ python -mtimeit 'for x in xrange(1000):pass' 1 loops, best o

Re: python loops

2006-09-08 Thread Alex Martelli
Tim Roberts <[EMAIL PROTECTED]> wrote: ... > xrange used to be better. As I understand it, that's no longer the case. measuring is better than guessing: brain:~/codejam alex$ python -V Python 2.5c1 brain:~/codejam alex$ python -mtimeit 'for x in range(1000):pass' 1 loops, best of 3: 71.9

Re: python loops

2006-09-04 Thread Nicko
Steve Holden wrote: > Nicko wrote: > > Fredrik Lundh wrote: > >>if you cannot refrain from pulling arguments out of your ass, you not > >>really the right person to talk about hygiene. > > > > I'm impressed but your mature argument. Clearly, in the face of such > > compelling reasoning, I shall hav

Re: python loops

2006-09-03 Thread Steve Holden
Nicko wrote: > Fredrik Lundh wrote: > >>Nicko wrote: >> >> >>>... In the case of the idiom "for i in >>>range(x):..." there absolutely no utility whatsoever in creating and >>>recording the list of objects. >> >>for short lists, both objects create the *same* number of objects. > > > This is tru

Re: python loops

2006-09-03 Thread Nicko
Fredrik Lundh wrote: > Nicko wrote: > > > ... In the case of the idiom "for i in > > range(x):..." there absolutely no utility whatsoever in creating and > > recording the list of objects. > > for short lists, both objects create the *same* number of objects. This is true for long lists too, if yo

Re: python loops

2006-09-03 Thread Fredrik Lundh
Nicko wrote: > There's a huge difference between not being profligate with resources > and premature optimisation. In the case of the idiom "for i in > range(x):..." there absolutely no utility whatsoever in creating and > recording the list of objects. Unless it makes a difference to code > struc

Re: python loops

2006-09-03 Thread Fredrik Lundh
Tim Roberts wrote: > xrange used to be better. As I understand it, that's no longer > the case. for short ranges, range is faster in some Python versions, xrange is faster in some (including 2.4). the difference is usually very small (the same number of objects are created in both cases). f

Re: python loops

2006-09-02 Thread Tim Roberts
[EMAIL PROTECTED] wrote: >AlbaClause wrote: > >> for i in range(length): >> print i > >Or usually better: > >for ii in xrange(length): >... xrange used to be better. As I understand it, that's no longer the case. -- - Tim Roberts, [EMAIL PROTECTED] Providenza & Boekelheide, Inc. -

Re: python loops

2006-09-02 Thread Nicko
Fredrik Lundh wrote: > [EMAIL PROTECTED] wrote: > > > I thought the xrange was preferred? for x in xrange(length): > > preferred by premature optimization freaks, perhaps. There's a huge difference between not being profligate with resources and premature optimisation. In the case of the idiom "f

Re: python loops

2006-09-01 Thread Paddy
Putty wrote: > In C and C++ and Java, the 'for' statement is a shortcut to make very > concise loops. In python, 'for' iterates over elements in a sequence. > Is there a way to do this in python that's more concise than 'while'? > > C: > for(i=0; i > > python: > while i < length: >

Re: python loops

2006-09-01 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote: > I thought the xrange was preferred? for x in xrange(length): preferred by premature optimization freaks, perhaps. in practice, if the range is reasonably small and you're going to loop over all the integers, it doesn't really matter. (the range form creates a list an

Re: python loops

2006-09-01 Thread bearophileHUGS
Kay Schluehr: > I hate ii ;) It's not nice looking, I agree. A more explicit name is often better. But I think ii is better than i because you can find it in the code (with the Find command of the editor or grep) more easily than i. Bye, bearophile -- http://mail.python.org/mailman/listinfo/py

Re: python loops

2006-09-01 Thread stdazi
`range' is especially useful for iterating over long sequences ;-) for i in range(0,100) : OverflowError: range() result has too many items Sybren Stuvel wrote: > [EMAIL PROTECTED] enlightened us with: > > I thought the xrange was preferred? for x in xrange(length): > > True. I

RE: python loops

2006-08-31 Thread Michael . Coll-Barth
> -Original Message- > From: Kay Schluehr > > > > > python: > > while i < length: > > i += 1 > > As AlbaClause had demonstrated you can iterate over indices as well > using the for-statement and the range() function. But you can also > combine iterating over elements

Re: python loops

2006-08-31 Thread Kay Schluehr
[EMAIL PROTECTED] wrote: > AlbaClause wrote: > > > for i in range(length): > > print i > > Or usually better: > > for ii in xrange(length): ~~ I hate ii ;) Regards, Kay -- http://mail.python.org/mailman/listinfo/python-list

Re: python loops

2006-08-31 Thread Kay Schluehr
Putty wrote: > In C and C++ and Java, the 'for' statement is a shortcut to make very > concise loops. In python, 'for' iterates over elements in a sequence. > Is there a way to do this in python that's more concise than 'while'? > > C: > for(i=0; i > > python: > while i < length: >

Re: python loops

2006-08-31 Thread bearophileHUGS
AlbaClause wrote: > for i in range(length): > print i Or usually better: for ii in xrange(length): ... Bye, bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: python loops

2006-08-31 Thread AlbaClause
Putty wrote: > In C and C++ and Java, the 'for' statement is a shortcut to make very > concise loops. In python, 'for' iterates over elements in a sequence. > Is there a way to do this in python that's more concise than 'while'? > > C: > for(i=0; i > > python: > while i < length: > i += 1 for