IF语句带有2个变量和4个条件

3
我有两个变量,假设为xy,我需要检查它们中哪一个是None
if 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 结构。


{btsdaf} - yash
7个回答

3
保持您使用的顺序:
if 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

修改顺序以减少布尔运算:
if 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)

0
如果你有两个布尔值,那么就有四种不同的可能性:00、01、10和11。如果每种情况下都需要发生截然不同的事情,那就没有什么可以减少测试次数的方法了。
话虽如此,在一些类似的情况下,使用“模式匹配”可能会更直观。不过,我不清楚在Python中是否有这样的功能。

0
  def 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)

输出:

100

{btsdaf} - Adirio
{btsdaf} - galaxyan
问题不在于定义函数时,而是在调用函数时。你在还不知道将调用哪个函数之前就传递了 100,所以所有的函数都需要处理一个参数(即使它们对此并没有做任何操作)。基本上,你需要获取每个函数的参数集合,并将它们的并集作为调用签名。输出也是一样的。 - Adirio
{btsdaf} - Adirio
{btsdaf} - Adirio
显示剩余4条评论

0
如果您需要每个可能的 xy 组合的 4 条不同路径,那么您无法真正简化它。话虽如此,我会将其中一个变量的检查分组(即首先检查 x 是否为 None,然后在内部检查 y 是否为 None)。
类似于这样的代码:
if 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

0

你可以使用elif语句开始编程,这样会更简洁一些,就像这样:

if 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`

0

假设您需要分别涵盖所有4种情况:

if 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)

这可能是处理它的最短途径,但不仅检查None/Null值。not x 对任何falsy值都返回true,例如空字符串,False或0。


1
{btsdaf} - tyteen4a03
{btsdaf} - ividito
1
不,我的意思是原始代码只检查None,而你的代码会让任何假值通过。 - tyteen4a03

-1
以下逻辑应该运行所有的组合。请用您的语言编写。
 if (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代码:

int 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");
    }
}

{btsdaf} - Neeraj Kumar
{btsdaf} - Adirio
{btsdaf} - Neeraj Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接