组合条件语句排列的最佳方法 [英] Best way to combine a permutation of conditional statements

查看:61
本文介绍了组合条件语句排列的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,基于4个条件变量,我要执行一系列操作-假设x,y,z& t。这些变量中的每一个都有可能的True或False值。因此,总共有16种可能的排列。而且我需要为每个排列执行不同的操作。

So, I have a series of actions to perform, based on 4 conditional variables - lets say x,y,z & t. Each of these variables have a possible True or False value. So, that is a total of 16 possible permutations. And I need to perform a different action for each permutation.

最好的方法是这样做,而不是构造一个庞大的if-else构造。

What is the best way to do this rather than making a huge if-else construct.

让我们看一个简化的例子。如果我尝试将所有不同的排列包含到一个大型if-else构造中,这就是我的代码。

Lets see a simplified example. This is how my code would look if I try to contain all the different permutations into a large if-else construct.

if (x == True):
    if (y == True):
        if (z == True):
            if (t == True):
                print ("Case 1")
            else:
                print ("Case 2")
        else:
            if (t == True):
                print ("Case 3")
            else:
                print ("Case 4")
    else:
        if (z == True):
            if (t == True):
                print ("Case 5")
            else:
                print ("Case 6")
        else:
            if (t == True):
                print ("Case 7")
            else:
                print ("Case 8")
else:
    if (y == True):
        if (z == True):
            if (t == True):
                print ("Case 9")
            else:
                print ("Case 10")
        else:
            if (t == True):
                print ("Case 11")
            else:
                print ("Case 12")
    else:
        if (z == True):
            if (t == True):
                print ("Case 13")
            else:
                print ("Case 14")
        else:
            if (t == True):
                print ("Case 15")
            else:
                print ("Case 16")

有什么方法可以简化此过程吗?显然,我针对每种情况的目标都比仅打印案例1更为复杂。

Is there any way to simplify this? Obviously, my objective for each case is more complicated than just printing "Case 1".

推荐答案

您可以使用案例图得出结果:

You can use a map of cases to results:

cases = { (True,  True,  True,  True):  "Case 1",
      (True,  True,  True,  False): "Case 2",
      (True,  True,  False, True): "Case 3",
      (True,  True,  False, False):"Case 4",
      (True,  False, True,  True): "Case 5",
      (True,  False, True,  False):"Case 6",
      (True,  False, False, True): "Case 7",
      (True,  False, False, False):"Case 8",
      (False, True,  True,  True): "Case 9",
      (False, True,  True,  False):"Case 10",
      (False, True,  False, True): "Case 11",
      (False, True,  False, False):"Case 12",
      (False, False, True,  True): "Case 13",
      (False, False, True,  False):"Case 14",
      (False, False, False, True): "Case 15",
      (False, False, False, False):"Case 16"}

print(cases[(x,y,z,t])

如果您想针对每种情况进行其他操作/不同操作,则可以向该图添加一个函数。

If you want to do something else/different for each case, you could add a function to that map.

cases = { (True,  True,  True,  True):  foo_func,
      (True,  True,  True,  False): bar_func,
         ...}

result = cases[(x,y,x,t)](*args)

您也可以使用以下之一屏蔽解决方案可以使代码更短,或者如果您要写的案例太多,但是对于较小的案例集,此显式表示将更清晰并且更易于维护。

You can also use one of the masking solutions to make the code shorter, or if you have too many cases to write out, but for smaller sets of cases, this explicit representation will be clearer and easier to maintain.

这篇关于组合条件语句排列的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆