Apply a function to each list member?

Darrell darrell at dorb.com
Thu Oct 21 18:16:26 EDT 1999


>>> map(f,l)
['AAA', 'BBB', 'CCC', 1, 2, 3, 'XXX', 'YYY', 'ZZZ']

--Darrell
----- Original Message ----- 
From: Gaetan Corneau <Gaetan_Corneau at baan.com>
To: 'Python list' <python-list at cwi.nl>
Sent: Thursday, October 21, 1999 5:20 PM
Subject: Apply a function to each list member?


> Hi,
> 
> Is there a way to do this (see "ListApply" below) with standard Python
> stuff?
> 
> def f(s):
>     from string import upper
>     from types import StringType
>     if type(s) == StringType:
>         return upper(s) 
>     else:
>         return s
> 
> 
> def ListApply(f, l):
>     for i in  range(0, len(l)):
>         l[i] = f(l[i])
> 
> 
> l = ["aaa", "bbb", "ccc", 1, 2, 3, "xxx", "yyy", "zzz"]
> print l
> 
> ListApply(f, l)
> print l
> 
> 
> Thanks,
> ______________________________________________________
>    Gaetan Corneau
>    Software Developer (Quality Assurance Team)
>    BaaN  Supply Chain Solutions  
>    E-mail: Gaetan_Corneau at baan.com        
>    Compuserve: Gaetan_Corneau at compuserve.com  
>    ICQ Number: 7395494             
>    Tel: (418) 266-8252          
> ______________________________________________________
> "Profanity is the one language all programmers know best"
> 
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list