A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0];
这些是变量,可以像这样思考函数
def hlf(A,B,C):
return A**(-1.0/2.0)-0.2*B-43+C
print "T:"
hlf(A,B,C)
首先,我想使用方程中 A B 和 C 的第一个值。在我想使用第二个值之后。我怎样才能做到这一点 ?
最佳答案
map
+ 列表
注意map
可以采用多个可迭代参数:
res = map(hlf, A, B, C)
[-34.86429773960448, -33.68377223398316]
在 Python 2.7 中,map
返回一个 list
。在 Python 3.x 中,map
返回一个迭代器,因此您可以延迟迭代或通过 list
进行穷举,即 list(map(hfl, A, B, C ))
。
map(function, iterable, ...)
...If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel.
zip
+ 列表理解
您可以在列表理解中使用zip
。为了清楚起见,您应该避免将参数命名为与变量相同的名称。
A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0];
def hlf(x, y, z):
return x**(-1.0/2.0) - 0.2*y - 43 + z
res = [hlf(*vars) for vars in zip(A, B, C)]
[-34.86429773960448, -33.68377223398316]
关于python - 同一方程中变量的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53206474/