Python 将map函数与多变量函数结合使用
我有一个多变量函数,我想使用map()函数 例如:Python 将map函数与多变量函数结合使用,python,dictionary,Python,Dictionary,我有一个多变量函数,我想使用map()函数 例如: def f1(a, b, c): return a+b+c map(f1, [[1,2,3],[4,5,6],[7,8,9]]) 你不能。使用包装器 def func1(a, b, c): return a+b+c map((lambda x: func1(*x)), [[1,2,3],[4,5,6],[7,8,9]]) 您可以简单地将多参数函数包装到另一个函数中,该函数只将一个参数作为元组/列表,然后将其传递给内部函数
def f1(a, b, c):
return a+b+c
map(f1, [[1,2,3],[4,5,6],[7,8,9]])
你不能。使用包装器
def func1(a, b, c):
return a+b+c
map((lambda x: func1(*x)), [[1,2,3],[4,5,6],[7,8,9]])
您可以简单地将多参数函数包装到另一个函数中,该函数只将一个参数作为元组/列表,然后将其传递给内部函数
map(lambda x: func(*x), [[1,2,3],[4,5,6],[7,8,9]])
为此而制作:
import itertools
def func1(a, b, c):
return a+b+c
print list(itertools.starmap(func1, [[1,2,3],[4,5,6],[7,8,9]]))
输出:
[6, 15, 24]