简体   繁体   中英

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

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. 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.). In the end, I want the function to return a list. Thanks!

For vanilla Python you have to use something like this:

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; it will throw an error instead. However, as @juanpa says, such an automatic conversion is performed by a python module called numpy .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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