0

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!

2
  • 1
    this should just work with numpy. With lists, you have to do the looping yourself, they do not implement vectorized operations. Commented Apr 6, 2020 at 9:45
  • @juanpa.arrivillaga Nice advice, it worked. Commented Apr 6, 2020 at 10:27

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, this did work. But for functions which contain '+' operators, an error comes out: unsupported operand type(s) for +: 'int' and 'list' . I did the calculation with another function return [(K*Smax*c)/(1 + K*c)) for c in c_list].
@Michael If that is the case, the problem on your code is not on the return [(K*Smax*c)/(1 + K*c)) for c in c_list] line. It is further up.. You are at some point adding an integer to a list. something like c_list = 5 + c_list or so
I am sorry that my expression in the question text is not clear enough and misleading. The 'n' is not a list, the 'c' is the only list instead. I understood your answer so I didn´t mention that you have mistaken them two.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.