简体   繁体   English

如何传递参数(元组)-不同长度

[英]How to pass parameters (tuple) - Different lengths

This question may seem very basic, however, I would like to improve the code I have written. 这个问题似乎很基础,但是,我想改进自己编写的代码。 I have a function that will need either 2 or 3 parameters, depending on some other conditions. 我有一个需要2或3个参数的函数,具体取决于其他一些条件。 I'm checking the length and passing either 2 or 3 with and if statement (see code). 我正在检查长度,并使用if语句传递2或3(请参见代码)。 I'm sure there must be a better and compact way to re-write in one line/sentence instead of using IIFs. 我敢肯定,必须有一种更好而紧凑的方式来重写一行而不是使用IIF。 Sorry i'm fairly new to python. 抱歉,我是python的新手。

dist = distr.gum 
# ... or: dist = distr.gev 

# parFitHist has length 2 for distr.gum or 3 for distr.gev 
parFitHist = dist.fit(ppt_hist) 

# now I check if 
if len(parFitBase) > 2: 
    # 3 parameters for distr.gev
    t_mp = dist.cdf(ppt_fut, parFitFut[0], parFitFut[1],  parFitBase[2]) 
else:
    # 2 parameters for distr.gum
    t_mp = dist.cdf(ppt_fut, parFitFut[0], parFitFut[1])  

Not sure what data types you have and how your method looks like, but with *args you can solve that: 不知道您拥有什么数据类型以及方法的样子,但是使用* args可以解决:

def cdf(ppt_fut, *params):
    print(ppt_fut)
    print(params)

Then you can call it like that: 然后,您可以这样称呼它:

cdf(1, 2, 3, 4) # -> prints: 1 (2,3,4)
cdf(1, 2, 3) # -> prints: 1 (2,3)

The params is in this case a tuple with all arguments except the first one. 在这种情况下, params是具有除第一个参数以外的所有参数的元组。

You can unpack elements of a list into a function call with * . 您可以使用*将列表的元素解压缩为函数调用。

You don't need to know how many items there are in the list to do this. 您无需知道列表中有多少项即可执行此操作。 But this means you can introduce errors if the number of items don't match the function arguments. 但这意味着,如果项数与函数参数不匹配,则会引入错误。 It's therefore a good idea to check your data for some basic sanity as well. 因此,最好还是检查一下您的数据是否基本正常。

For example: 例如:

if 1 < len(parFitBase) < 4:
    t_mp = dist.cdf(ppt_fut, *parFitFut)
else:
    raise ValueError('Array must have length 2 or 3')

You can read more about that here: Unpacking Argument Lists 您可以在此处阅读有关此内容的更多信息: 解压缩参数列表

Welcome to python. 欢迎使用python。 Please consider using pep8 for formatting your code. 请考虑使用pep8格式化代码。 It is a great way to respect conventions and make your code more readable for other people. 这是遵守约定并使您的代码对其他人更具可读性的好方法。

Variable names should be lower case and separated by underscores. 变量名称应为小写字母,并用下划线分隔。

Regarding your question, one way of improving it could be to pass len(par_fit_base) to your function and doing the if clause there. 关于您的问题,一种改进方法是将len(par_fit_base)传递给函数并在其中执行if子句。 This would make it more fluent to read the code. 这将使阅读代码更加流利。 This is assuming that par_fit_fut always has the same length, regardless of par_fit_base . 这是假设par_fit_fut始终具有相同的长度,而与par_fit_base

def my_function(par_fit_fut, n):
    if n > 2:
        # one set of routines using par_fit_fut[0:3]
    else:
        # another set of routines using par_fit_fut[0:2]

my_function(par_fit_fut, len(par_fit_base))

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

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