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

时间:2017-10-23 13:26:33

标签: python python-3.x if-statement

我有2个变量,比如 x y ,我需要检查哪些是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结构。

7 个答案:

答案 0 :(得分:2)

保持您使用的订单:

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)

答案 1 :(得分: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

答案 2 :(得分:0)

如果xy的每种可能组合需要4条不同的路径,那么您无法真正简化它。也就是说,我会对其中一个变量进行分组检查(例如,首先检查x is None是否为y is 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 。)

这样的事情:

(?:\d+)((\d{1,3})*([\,\ ]\d{3})*)(\.\d+)?

答案 3 :(得分: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`

答案 4 :(得分:0)

如果您有两个布尔,则有四种不同的可能性:00 01 10和11.如果在每种情况下都应该发生明显不同的事情,那么< strong>不是真正可以减少测试次数的东西。唯一的方法是引入三个if语句(如1&amp; 1,0&amp; 1,1&amp; 0)和一个默认值(将捕获00),为您节省一个比较。在绝大多数情况下,这可能是无关紧要的,甚至可能是编译器或解释器所做的事情。 至少我对最近看到的gcc-magic的一些展示感到困惑。

答案 5 :(得分: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都会返回true,例如空字符串,False或0。

答案 6 :(得分:-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");
    }
}