且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

多处理具有多个输入的函数

更新时间:2023-11-18 08:04:52

有几种方法可以做到这一点.在问题中给出的示例中,您可以只定义一个包装函数

There are several ways to do this. In the example given in the question, you could just define a wrapper function

def g(i):
    return f(i, 20)

并将这个包装器传递给 map().更通用的方法是有一个包装器,它接受一个元组参数并将元组解包为多个参数

and pass this wrapper to map(). A more general approach is to have a wrapper that takes a single tuple argument and unpacks the tuple to multiple arguments

def g(tup):
    return f(*tup)

或使用等效的 lambda 表达式:lambda tup: f(*tup).

or use a equivalent lambda expression: lambda tup: f(*tup).