is there a better way...

holger krekel pyth at devel.trillke.net
Fri May 10 11:32:37 EDT 2002


Christopher Encapera wrote:
> 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?

def subst(elem):
    if elem==5:
        elem='678'
    return elem

m= map(subst, [1,2,3,4,5,6,7,8,9])
>>> print m
[1, 2, 3, 4, '678', 6, 7, 8, 9]

> (My goal is to open Windows logon scripts and update the path to the latest
> virus DAT's)

noble :-)

   holger





More information about the Python-list mailing list