Python: apply list of functions to each element in

2019-05-14 00:09发布

Say I have list with elements content = ['121\n', '12\n', '2\n', '322\n'] and list with functions fnl = [str.strip, int].

So I need to apply each function from fnl to each element from content sequentially. I can do this by several calls map.

Another way:

xl = lambda func, content: map(func, content)
for func in fnl:
    content = xl(func, content) 

I'm just wondering if there is a more pythonic way to do it.

Without separate function? By single expression?

2条回答
叛逆
2楼-- · 2019-05-14 00:38

You could use the reduce() function in a list comprehension here:

[reduce(lambda v, f: f(v), fnl, element) for element in content]

Demo:

>>> content = ['121\n', '12\n', '2\n', '322\n']
>>> fnl = [str.strip, int]
>>> [reduce(lambda v, f: f(v), fnl, element) for element in content]
[121, 12, 2, 322]

This applies each function in turn to each element, as if you nested the calls; for fnl = [str.strip, int] that translates to int(str.strip(element)).

In Python 3, reduce() was moved to the functools module; for forwards compatibility, you can import it from that module from Python 2.6 onwards:

from functools import reduce

results = [reduce(lambda v, f: f(v), fnl, element) for element in content]

Note that for the int() function, it doesn't matter if there is extra whitespace around the digits; int('121\n') works without stripping of the newline.

查看更多
虎瘦雄心在
3楼-- · 2019-05-14 00:59

You are describing the basic use of a list comprehension:

>>> content = ['121\n', '12\n', '2\n', '322\n']
>>> [int(n) for n in content]
[121, 12, 2, 322]

Note you don't need the call to strip to convert to integer here, some whitespace is handled fine.

If your real use-case is more complex and you wish to compose arbitrarily many functions in the comprehension, however, I found the idea from here quite pythonic:

def compose(f1, f2):
    def composition(*args, **kwargs):
        return f1(f2(*args, **kwargs))
    return composition

def compose_many(*funcs):
    return reduce(compose, funcs)
查看更多
登录 后发表回答