简体   繁体   English

Python中的一个function如何输入不同类型的参数?

[英]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.

相关问题 如何实现具有不同参数类型的python函数 - How implement a python function with different parameter types 在python中,如何使输入参数成为不可变的函数? - In python, how does one make an input parameter to function immutable? 如何为同一个功能使用不同的输入类型? - How to have different input types for the same function? 如何记录python函数参数类型? - How to document python function parameter types? Python TypeHint a function 根据输入返回不同类型 - Python TypeHint a function that returns different types based on input 在Python中,为同一事物的不同类型使用两个参数还是处理多个类型的一个参数更好? - In Python, is it better to have two parameters for different types of the same thing, or handle one parameter of multiple types? python:检查函数参数类型 - python: check function parameter types Python - 在一个函数中展平两种不同类型的列表 - Python - Flatten lists of lists of two different types in one function 如何在python中将函数保存为输入参数? - How to save a function as input parameter in python? Flask (Python):将输入作为参数传递给具有固定 URL 的不同路由的函数 - Flask (Python): Pass input as parameter to function of different route with fixed URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM