我有一个多变量函数,我想使用它的map()函数。使用具有多变量函数的映射函数
例子:
def f1(a, b, c):
return a+b+c
map(f1, [[1,2,3],[4,5,6],[7,8,9]])
我有一个多变量函数,我想使用它的map()函数。使用具有多变量函数的映射函数
例子:
def f1(a, b, c):
return a+b+c
map(f1, [[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]
你不能。使用包装。
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]])