python - 同一方程中变量的多个值

标签 python python-2.7

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 ))

Reference :

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/

相关文章:

python - 属性错误 : Message instance has no attribute 'pack'

python - 使用 matplotlib 在 Python 中未显示绘图

python - python 中的多重继承以及向继承类传递参数

python - 如何在 matplotlib 散点图中标准化颜色图?

python - 了解 Numpy 多维数组索引

python - 如何在Vpython中添加纹理?

python - 如何解析 Python (2.7) 中可能有或没有小数秒的时间?

python pandas - 将值输入到新列中

python - 如何让 Flake8 处理 F811 错误?

python - 将日期时间戳转换为本地纪元日期时间