简体   繁体   English

同一方程式中变量的多个值

[英]multiple values of a variable in same equation

A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0];

these are the variables and think about function like 这些是变量,并考虑像

def hlf(A,B,C):
       return A**(-1.0/2.0)-0.2*B-43+C
print "T:"
hlf(A,B,C)

Firstly, I want to use first values of the AB and C in the equation. 首先,我想在方程式中使用AB和C的第一个值。 After I want to use second values. 之后,我想使用第二个值。 How can I do this ? 我怎样才能做到这一点 ?

map + list map + list

Note map can take multiple iterable arguments: 注释map可以采用多个可迭代的参数:

res = map(hlf, A, B, C)

[-34.86429773960448, -33.68377223398316]

In Python 2.7, map returns a list . 在Python 2.7中, map返回一个list In Python 3.x map returns an iterator, so you can either iterate lazily or exhaust via list , ie list(map(hfl, A, B, C)) . 在Python 3.x中, map返回一个迭代器,因此您可以懒惰地迭代,也可以通过list用尽,例如list(map(hfl, A, B, C))

Reference : 参考

map ( function , iterable , ...) mapfunctioniterable ,...)

...If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. ...如果传递了其他可迭代的参数,则函数必须采用那么多参数,并且并行地将其应用于所有可迭代的项目。

zip + list comprehension zip +列表理解

You can use zip within a list comprehension. 您可以在列表推导中使用zip For clarity, you should avoid naming your arguments the same as your variables. 为了清楚起见,应避免将参数命名为与变量相同的名称。

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]

Vectorize with Numpy. 用Numpy向量化。 Best Performace 最佳表现

Normally its much better try to vectorize this kind of operations with numpy, because the best performance results. 通常,最好使用numpy将这种操作向量化,因为会获得最佳性能。 When you vectorize instead to use a loop, you are using all your cores, and its the fastest solution. 当向量化而不是使用循环时,您将使用所有内核及其最快的解决方案。 You should vectorize the operation with numpy. 您应该使用numpy将操作向量化。 Something like this: 像这样:

import numpy as np

A = [18.0,10.0]; B = [13.0,15.0]; C = [10.5,12.0];
a = np.array(A)
b = np.array(B)
c = np.array(C)

And now your function with the new vectors like arguments: 现在,您的函数有了新的向量,例如参数:

def hlf(a_vector,b_vector,c_vector):
    return a_vector**(-1.0/2.0)-0.2*b_vector-43+c_vector

And finally call your new function vectorized: 最后调用向量化的新函数:

print (hlf(a_vector = a,b_vector = b,c_vector = c))

Output: 输出:

>>> array([-34.86429774, -33.68377223])

If you want to keep your function as is, you should call it N times with: 如果要保持功能不变,则应使用以下命令调用N次:

for i in range(N):
    result = hlf(A[i], B[i], C[i])
    print(result)

Another interesting method is to make a generator with your function: 另一个有趣的方法是使用您的函数生成一个生成器:

A = [18.0,10.0]
B = [13.0,15.0]
C = [10.5,12.0];

def hlf(*args):
    i=0
    while i < len(args[0]):
        yield args[0][i]**(-1.0/2.0) - 0.2*args[1][i] - 43 + args[2][i]
        i += 1

results = hlf(A, B, C)
for r in results:
    print(r)

Output: 输出:

-34.86429773960448
-33.68377223398316

Last one is rather edicational if you want to practice python generators. 如果您想练习python生成器,那么最后一个非常实用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM