Re: Getting number of iteration

2005-05-06 Thread Fredrik Lundh
Mike Meyer wrote:

> > n = 0
> > for x in lst:
> > print "iteration %d on element %s" % (n, x)
> > n += 1
>
> Just for the record, the old idiom was:
>
> for n in xrange(len(lst)):
> x = lst[n]
> print "iteration %d on element %s" % (n, x)

it was?  of the following four solutions,

for n in xrange(len(lst)):
x = lst[n]
...

for n in range(len(lst)):
x = lst[n]
...

n = 0
for x in lst:
...
n += 1

for x, n in enumerate(lst):
...

the xrange solution tends to be the slowest, especially for
relatively short lists (up to a 1000 elements, or so).

the exact details vary somewhat between Python versions,
but the += solution is always a good choice, and the xrange
solution is almost always a bad choice.





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting number of iteration

2005-05-06 Thread Mike Meyer
Bill Mill <[EMAIL PROTECTED]> writes:

> On 5/6/05, Florian Lindner <[EMAIL PROTECTED]> wrote:
>> Hello,
>> when I'm iterating through a list with:
>> 
>> for x in list:
>> 
>> how can I get the number of the current iteration?
> Earlier:
>
> n = 0
> for x in lst:
> print "iteration %d on element %s" % (n, x)
> n += 1

Just for the record, the old idiom was:

for n in xrange(len(lst)):
x = lst[n]
print "iteration %d on element %s" % (n, x)

  http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting number of iteration

2005-05-06 Thread George Sakkis
"Florian Lindner" wrote:

> Hello,
> when I'm iterating through a list with:
>
> for x in list:
>
> how can I get the number of the current iteration?
>
> Thx,
>
> Florianfor

in python 2.3+:

for i,x in enumerate(sequence):
print "sequence[%d] = %s" %(i,x)


George

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting number of iteration

2005-05-06 Thread Bill Mill
On 5/6/05, Bill Mill <[EMAIL PROTECTED]> wrote:
> On 5/6/05, Florian Lindner <[EMAIL PROTECTED]> wrote:
> > Hello,
> > when I'm iterating through a list with:
> >
> > for x in list:
> >
> > how can I get the number of the current iteration?
> 
> Python 2.4 and greater:

ummm, make that 2.3 and greater. I always think things are more recent
than they are.

> 
> for n, x in enumerate(lst):
> print "iteration %d on element %s" % (n, x)
> 
> Earlier:
> 
> n = 0
> for x in lst:
> print "iteration %d on element %s" % (n, x)
> n += 1
> 
> And you shouldn't use list as a variable name; list() is a built-in
> function which you'll clobber if you do.
> 
> Peace
> Bill Mill
> bill.mill at gmail.com
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting number of iteration

2005-05-06 Thread Bill Mill
On 5/6/05, Florian Lindner <[EMAIL PROTECTED]> wrote:
> Hello,
> when I'm iterating through a list with:
> 
> for x in list:
> 
> how can I get the number of the current iteration?

Python 2.4 and greater:

for n, x in enumerate(lst):
print "iteration %d on element %s" % (n, x)

Earlier:

n = 0
for x in lst:
print "iteration %d on element %s" % (n, x)
n += 1

And you shouldn't use list as a variable name; list() is a built-in
function which you'll clobber if you do.

Peace
Bill Mill
bill.mill at gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Getting number of iteration

2005-05-06 Thread Florian Lindner
Hello,
when I'm iterating through a list with:

for x in list:

how can I get the number of the current iteration?

Thx,

Florian
-- 
http://mail.python.org/mailman/listinfo/python-list