indexed looping, functional replacement?
Brian O. Bush
bushbo at attbi.com
Sat Nov 9 19:26:09 EST 2002
bushbo at attbi.com (Brian O. Bush) wrote in message news:<3fe406f0.0211091037.2795e538 at posting.google.com>...
> I currently have a list of functions and a list of arguments to the
> functions (n to n). Is there a way I can get around having to keep an
> index into the arguments as I loop through the functions.
First, thanks for all your feedback.
I was playing around and came up with some alternative solutions:
def f1(str):
return float(str)
def f2(str):
return float(str) - 1
def f3(str):
return float(str)
str = ["2.144", "2.343", "3.14159"]
fn = [f1, f2, f3]
an alternative imperative approach:
z = []
for i in range(len(fn)):
z.append(fn[i](str[i]))
print z
and a functional approach, which I like alot:
z = map(lambda f, arg: f(arg), fn, str)
print z
Cheers,
Brian
More information about the Python-list
mailing list