views:

72

answers:

5

I have the following Python code:

data  = ['1', '4.6', 'txt']
funcs = [int, float, str]

How to call every function with data in corresponding index as an argument to the function? Now I'm using the code:

result = []
for i, func in enumerate(funcs):
    result.append(func(data[i]))

map(funcs, data) don't work with tuple of function ( Is there builtin function to do that simpler?

+6  A: 

You could use zip* to combine many sequences together:

zip([a,b,c,...], [x,y,z,...]) == [(a,x), (b,y), (c,z), ...]

then you could iterate on this new sequence and make each function apply on the corresponding data. Since you just want to collect them into a list, list comprehension is much better than a for-loop:

result = [f(x) for f, x in zip(funcs, data)]

Note: * Use itertools.izip if you are using Python 2.x and the lists are very long.)

KennyTM
+2  A: 

[f(d) for d,f in zip(data, funcs)]

Knio
+2  A: 
>>> data  = ['1', '4.6', 'txt']
>>> funcs = [int, float, str]
>>> result = [funcs[pos](x) for pos, x in enumerate(data)]
>>> result
[1, 4.5999999999999996, 'txt']
>>> 
pyfunc
A: 

If you need the values one by one, you can also create generator for values:

def my_funcvals(funcs,vals):
    return ("%s(%r) = %r" %(f.__name__,d, f(d)) for d,f in zip(data, funcs))

data  = ['1', '4.6', 'txt']
funcs = [int, float, str]

for result in my_funcvals(funcs, data):
    print result
Tony Veijalainen
It's not necessary to write a function. Just take either of the list comprehension solutions and use a generator expression. `(f(x) for f, x in zip(funcs, data))`
Glenn Maynard
I thought it more nice to write function which can put in utility module. I assumed that probably this kind of generation happens more than once. So it is good practice not to inline the generator, as generators can not be restarted, but must be 'regenerated'. Then the main module become more consice and the code is easier to reuse. I also did the str style human readable print strings return. This could be easily transformed to object.
Tony Veijalainen
+1  A: 

map() will work with sequences of functions, although perhaps not in the way you thought:

data  = ['1', '4.6', 'txt']
funcs = [int, float, str]

result = map(lambda f,d: f(d), funcs, data)
# or
result = map(lambda d,f: f(d), data, funcs)
martineau