3

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 lists of functions ( Is there builtin function to do that simpler?

5 Answers 5

9

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.)

Sign up to request clarification or add additional context in comments.

Comments

3

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 = list(map(lambda f,d: f(d), funcs, data))
# or
result = list(map(lambda d,f: f(d), data, funcs))

Comments

2

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

Comments

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

Comments

0

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

2 Comments

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))
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.