[英]How to input different types of parameter to one function in Python?
I wanted to make my own specified module in Python for scientific work and a crucial step is to design my function.我想在 Python 中制作自己指定的模块用于科学工作,关键的一步是设计我的 function。 For example, I would like to build a Freundlich adsorption isotherm function: output = K*(c^n), with K and n as constants, c is the concentration of a compound (variable).
例如,我想建立一个Freundlich吸附等温线function:output = K*(c^n),其中K和n为常数,Z4A8A08F09D37B7379564903840为化合物的浓度。
def Freundlich(K, c, n):
ads_Freundlich = K * c ** n
return ads_Freundlich
However, with these codes I could only input K, c, n all as single figures.但是,使用这些代码我只能输入 K,c,n 都作为单个数字。 I would like to know how can I run a function by giving the constant(s) as figures and the variable(s) as lists (or pandas series, etc.).
我想知道如何通过将常量作为数字和变量作为列表(或 pandas 系列等)来运行 function。 In the end, I want the function to return a list.
最后,我希望 function 返回一个列表。 Thanks!
谢谢!
For vanilla Python you have to use something like this:对于香草Python 你必须使用这样的东西:
def Freundlich(K, c, n_list):
return [K * c ** n for n in n_list]
If you pass a list to the function you wrote, it will not be automatically vectorized as you seem to expect;如果您将列表传递给您编写的 function,它不会像您预期的那样自动矢量化; it will throw an error instead.
它会抛出一个错误。 However, as @juanpa says, such an automatic conversion is performed by a python module called
numpy
.但是,正如@juanpa所说,这种自动转换是由名为
numpy
的 python 模块执行的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.