我将以下函数作为脚本inspired by this的一部分:
def view(a='', b='', c=''):
if a=='All' and b=='All' and c=='All': return df
if a=='All' and c=='All' and b!='All': return df[(df['b']==b)]
if a!='All' and c=='All' and b=='All': return df[(df['a']==a)]
if a=='All' and c!='All' and b=='All': return df[(df['c']==c)]
if a=='All' and c!='All' and b!='All': return df[(df['c']==c) & (df['b']==b)]
if a!='All' and c=='All' and b!='All': return df[(df['a']==a) & (df['b']==b)]
if a!='All' and c!='All' and b=='All': return df[(df['a']==a) & (df['c']==c)]
return df[(df['a']==a) & (df['b']==b) & (df['c']==c)]
有没有一种很好的方法可以用一个很好的pythonic表达式来编写所有的if语句。如果对n个变量进行推广,则得到额外答案。你知道吗
注意:Perhaps related to this question,但我还是搞不懂。你知道吗
您的功能基本上是这样:
让我们先从所有非“all”参数的列表开始重写:
障碍1:我们现在已经不知道哪个值与哪个参数匹配了。为什么我们要知道这些?这样我们就可以将参数与
df
的正确元素进行比较。我们可以通过将参数值和名称存储在notall
中来解决这个问题:将每个参数的名称写两次是很难看的,但要么是这样,要么就是用
locals
和/或**kwargs
调皮。你知道吗考虑到这一点,与
df
元素的比较很容易:现在,我们如何和他们在一起?我们可以使用^{} 和^{} ,但是(除非您重载了} 。你知道吗
==
以返回一个非布尔值,我希望您没有这样做),compared
的元素都是布尔值,这意味着将它们与按位and组合与将它们与逻辑and组合相同,Python已经有了一个函数:^{综合起来:
或者更紧凑:
现在,关于前面提到的调皮行为:如果所有参数都在
dict
中,那么notall
可以只包含键,这将允许我们查找参数值和df
值,而不会重复我们自己(太多)。如何获得dict
中的所有参数?使用**kwargs
:(注意使用
get
为参数提供默认值)但是NAMES
应该是什么?它不能是kwargs.keys()
,因为它只包含用户传入的参数,可能不是所有参数(甚至可能包含我们不期望的键!)。选项1是在某处写出一个参数名列表,并使用:或者,如果
df
的键恰好与函数参数的所需名称相同,我们可以使用df.keys()
:或者,稍微短一点:
在此之后,我们只需要更新
notall
元素的使用方式,更改如下:对此:
(注意,我们仍然需要继续使用
get
来设置默认值。)把一切重新组合起来:
或者,如果参数名与
df
的键相同:编辑:根据下面的注释,
df
的值显然是覆盖==
的值,因此它不会返回布尔值。幸运的是,如上所述,这只需要改变这一点:对此:
这应该可以做到:
唯一的问题是,我知道没有一种简单的方法可以知道您当前使用的变量的名称。这就是我使用
df[var] == var
的原因。你知道吗例如,通过使每个变量都带有自己的名称来解决这个问题相对容易。所以,基本上,每个变量都是一个元组
a = (variable, "variable")
。你知道吗相关问题 更多 >
编程相关推荐