is there a better way...
holger krekel
pyth at devel.trillke.net
Fri May 10 15:11:19 EDT 2002
Eddie Corns wrote:
> Christopher Encapera <ChrisE at lantech.com> writes:
>
> >to loop over a list and replace an element based on its value than:
>
> >x = [1,2,3,4,5,6,7,8,9]
> >y = len(x)
> >z = 0
> >while z < y:
> > element = x[z]
> > if element == 5: x[z] = '678'
> > z = z +1
> >
>
> >Another words is there a way to do this with a "for" statement - some method
> >that will return the current z (of x[z]) in the loop?
> >(My goal is to open Windows logon scripts and update the path to the latest
> >virus DAT's)
>
> I have the following routine defined in my utils library
>
> from __future__ import generators
> # add sequence numbers to a sequence.
> # Instead of doing eg:
> # for a,b in seq:
> # do:
> # for n,(a,b) in number_seq (seq):
> def number_seq (seq):
> cnt = 0
> for item in seq:
> yield (cnt, item)
> cnt += 1
> ...
> Needs 2.2 obviously and a better name probably.
look at PEP279
http://www.python.org/peps/pep-0279.html
which will be in 2.3 and is a more performant
implementation than yours.
have fun,
holger
More information about the Python-list
mailing list