我有两个变量,比如x和y,我需要检查其中哪个是None。
1
2
3
4
5
6
7
8
9
10if x is None and y is None:
# code here
else:
if x is not None and y is not None:
# code here
else:
if x is None:
# code here
if y is None:
# code here
有没有更好的方法?我在找一个较短的IF ELSE结构。
另外,请注意,在Python中不存在空值。有关详细信息,请参阅stackoverflow.com/questions/3289601/null-object-in-python
保持您使用的顺序:
1
2
3
4
5
6
7
8if x is None and y is None:
# code here for x = None = y
elif x is not None and y is not None:
# code here for x != None != y
elif x is None:
# code here for x = None != y
else:
# code here for x != None = y
修改顺序以减少布尔值计算:
1
2
3
4
5
6
7
8if x is None and y is None:
# code here for x = None = y
elif x is None:
# code here for x = None != y
elif y is None:
# code here for x != None = y
else:
# code here for x != None != y
在4种情况下,您应该考虑哪个选项的概率更高,哪个选项的概率更高,并将它们保留在前两个选项中,因为这将减少执行期间检查的条件数量。最后两个选项都将执行3个条件,因此这两个选项的顺序无关紧要。例如,上面的第一个代码认为prob(x=None & y=None) > prob(x!=None & y!=None) > prob(x=None & y!=None) ~ prob(x!=None & y=None),而第二个代码认为prob(x=None & y=None) > prob(x=None & y!=None) > prob(x!=None & y=None) ~ prob(x!=None & y!=None)。
假设您需要将所有4个案例单独涵盖:
1
2
3
4
5
6
7
8if not x and not y:
# code here (both are none or 0)
elif not x
# code here (x is none or 0)
elif not y
# code here (y is none or 0)
else
#code here (both x and y have values)
这是处理它的最短方法,但不仅检查无/空值。not x对于任何错误都返回true,例如空字符串、False或0。
注意,not (False)是正确的。
这是正确的,请参阅stackoverflow.com/questions/20420934/…,了解有关如何在python中处理if x:内容的详细信息。
不,我要说的是,原始代码只要求None检查,而您的代码将允许任何错误通过。
如果你有两个乳房,有四种不同的可能性:00 01 10和11。如果在每种情况下都会发生明显不同的事情,那么就没有真正的事情可以减少测试的数量。唯一的方法是引入三个if语句(如1&1、0&1、1&0)和一个默认值(将捕获00),从而节省一个比较。在绝大多数情况下,这可能是不相关的,甚至可能是编译器或解释器做的任何事情。至少我被最近看到的海湾合作委员会的一些魔术表演搞糊涂了。
您可以从使用elif语句开始,它会稍微短一点,如下所示:
1
2
3
4
5
6
7
8if x is None and y is None:
# code here
elif x is not None and y is not None:
# code here
elif x is None:
# code here
if y is None:
# code here`
如果每个可能的x和y组合需要4个不同的路径,那么就不能真正简化它。也就是说,我将对其中一个变量的检查进行分组(即先检查x is None,然后在内部检查y is None)。
像这样:
1
2
3
4
5
6
7
8
9
10if x is None:
if y is None:
# x is None, y is None
else:
# x is None, y is not None
else:
if y is None:
# x is not None, y is None
else:
# x is not None, y is not None
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15def func1(a):
print a
def func2(a):
print a
def func3(a):
print a
def func4(a):
print a
options = {(1, 1): func1,
(1, 0): func2,
(0, 1): func3,
(0, 0): func4,}
options[(True, True)](100)
输出:
1100
要求内部所有函数的签名都相等
@不,不是的。它可以更改为*args
问题不在于定义函数时,而在于调用函数时。您在知道将调用哪个函数之前传递了100,因此所有这些函数都需要处理1个参数(即使它们没有对它做任何操作)。基本上,您需要获取每个函数的参数集,并获取所有参数集的并集,以获得调用签名。输出相同。
@阿迪里奥,这只是一个例子。您可以通过更改函数定义,将任何参数传递给您想要的函数。Python不会限制你在那一边
@但是如果func1需要参数"foo", 1和func2需要参数False怎么办?你会怎么写options[(x, y)](...)行?您可以使用lambda或partial,但这不会使代码变得更简单。
@adirio如果将函数定义为func(*args),则可以向函数传递任何内容。我不确定你是否理解"args"和"kwargs"
@Galaxyan我知道*args和**kwargs是如何工作的,我说的是函数调用。在您的示例中,您使用(100)来命名它。假设第一个选项需要一个元组列表作为参数,第二个选项需要一个可调用的元组和一个参数作为回调的元组,第三个选项是Nothing,第四个选项是特定对象。然后,您需要调用带有元组列表的函数、可调用的、带有参数的元组和对象;并确定函数内部哪些是usful,哪些不是。如果签名相同,就很简单,但是使用不同的签名会很快变得复杂。
@如果它变得非常复杂,那么您的if语句也会如此。
if ... elif ... else ...语句的复杂性来自条件,而可调用文件的dict来自内部签名,包括输入和输出。这两种方法都是有效的,当签名相似时,我使用这种方法。
下面的逻辑应该针对所有组合运行。用你的语言写
1
2
3
4
5
6
7
8
9
10
11
12
13if (x is not null and y is not null){
//code here
}
else if(x is null and y is not null){
//code here
}
else if(x is not null and y is null){
//code here
}
else
{
//Code here
}
相同的C代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19int main()
{
int x,y;
printf("Enetr Two Number");
scanf("%d%d",&x,&y);
if(x!=NULL && y!=NULL)
printf("X and Y are not null");
else if(x==NULL && y!=NULL)
printf("X is null and Y is not null");
else if(x!=NULL && y==NULL)
printf("X is not null and Y is null");
else
{
printf("The value of x and y are null");
}
}
@阿迪里奥非常感谢你的纠正。你说得对。我已经更新了我的答案。
否决票:他要求的是python,不是伪代码,也不是C。
@阿迪里奥,我想帮助理解逻辑,而不是提供准确的代码。