Say I have list with elements content = ['121\n', '12\n', '2\n', '322\n']
and list with functions fnl = [str.strip, int]
.
假设我有元素content = ['121 \ n','12 \ n','2 \ n','322 \ n']列表,并列出函数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
.
所以我需要将fnl中的每个函数依次应用于内容中的每个元素。我可以通过几个调用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.
我只是想知道是否有更多的pythonic方式来做到这一点。
Without separate function? By single expression?
没有单独的功能?通过单一表达?
2 个解决方案
#1
You could use the reduce()
function in a list comprehension here:
您可以在列表推导中使用reduce()函数:
[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))
.
这会依次将每个函数应用于每个元素,就像嵌套调用一样; for fnl = [str.strip,int]转换为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:
在Python 3中,reduce()被移动到functools模块;为了向前兼容,您可以从Python 2.6开始从该模块导入它:
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.
请注意,对于int()函数,数字周围是否有额外的空格并不重要; int('121 \ n')可以在不删除换行符的情况下工作。
#2
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.
请注意,这里不需要调用strip来转换为整数,一些空格处理得很好。
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:
如果您的真实用例更复杂,并且您希望在理解中任意编写许多函数,那么,我发现这里的想法非常pythonic:
def compose(f1, f2):
def composition(*args, **kwargs):
return f1(f2(*args, **kwargs))
return composition
def compose_many(*funcs):
return reduce(compose, funcs)
#1
You could use the reduce()
function in a list comprehension here:
您可以在列表推导中使用reduce()函数:
[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))
.
这会依次将每个函数应用于每个元素,就像嵌套调用一样; for fnl = [str.strip,int]转换为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:
在Python 3中,reduce()被移动到functools模块;为了向前兼容,您可以从Python 2.6开始从该模块导入它:
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.
请注意,对于int()函数,数字周围是否有额外的空格并不重要; int('121 \ n')可以在不删除换行符的情况下工作。
#2
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.
请注意,这里不需要调用strip来转换为整数,一些空格处理得很好。
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:
如果您的真实用例更复杂,并且您希望在理解中任意编写许多函数,那么,我发现这里的想法非常pythonic:
def compose(f1, f2):
def composition(*args, **kwargs):
return f1(f2(*args, **kwargs))
return composition
def compose_many(*funcs):
return reduce(compose, funcs)