Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使这个If循环成为对Python有效的字典_Python_If Statement_Switch Statement_Combinations_Boolean Logic - Fatal编程技术网

如何使这个If循环成为对Python有效的字典

如何使这个If循环成为对Python有效的字典,python,if-statement,switch-statement,combinations,boolean-logic,Python,If Statement,Switch Statement,Combinations,Boolean Logic,我正在从事一个有趣的小python项目,我一直在尝试将我的If循环改编成Dictionary,但我不确定如何实现这个函数,因为我需要测试多个条件 我有六个布尔,bool1-bool6可以是T或F,还有 我需要测试这些布尔值的每一个可能的组合,这样我就可以告诉我的程序在哪里绘制图像 有64种可能的组合 我们可以用3个布尔值来实现这一点,为了简单起见,3个布尔值有8种可能的组合 如果我们假设1=真,0=假,那么可能的组合可以这样表示 000 001 010 011 100 101 110 111

我正在从事一个有趣的小python项目,我一直在尝试将我的If循环改编成Dictionary,但我不确定如何实现这个函数,因为我需要测试多个条件

我有六个布尔,bool1-bool6可以是T或F,还有 我需要测试这些布尔值的每一个可能的组合,这样我就可以告诉我的程序在哪里绘制图像

有64种可能的组合

我们可以用3个布尔值来实现这一点,为了简单起见,3个布尔值有8种可能的组合

如果我们假设1=真,0=假,那么可能的组合可以这样表示

000
001
010
011
100
101
110
111
表示这一点的if循环是

if (bool1==false and bool2==false and bool3==false)
      do stuff
elif (bool1==false and bool2==false and bool3==true)
     do stuff
elif (bool1==false and bool2==true and bool3==false)
     do stuff

and so on...
除非你能找到一种简化这个过程的方法,明白我需要检查所有可能的布尔组合,否则没有必要批评我的问题。我只是不知道如何从这里取得进展,并将非常感谢一些帮助

我已经编写了一个64语句If循环,目前正在研究该解决方案,尽管我和我的cpu都希望使用更快的方法。

这是一个使用整数位表示的完美示例 你有六个布尔变量bool1到bool6

Want you Want是一个位表示,如下例所示:

101101
其中最左边的位代表bool6,最右边的位代表bool1。 所以你得到的顺序是bool6,bool5,bool4,…,bool1

现在,您将使用状态分配一个变量,该状态由以下布尔变量表示:

state = bool6 << 5
state = state ^ (bool5 << 4)
state = state ^ (bool4 << 3)
state = state ^ (bool3 << 2)
state = state ^ (bool2 << 1)
state = state ^ (bool1)
其中,字典键是从000000到111111的位

此外,您还可以检查每个案例是否都有一个已定义的参数:

for n in range(1<<6):
    if not n in cases:
       raise LookupError('cases do not contain index %d' % n)
cases = {
    0b000000: ((arg1a, arg2y), {'key1': kwarg1, 'key2': kwarg2}),
    0b000001: ((arg1b, arg2x), {'key23': kwarg23, 'key7': kwarg7}),
    # ...
    0b111111: ((arg1a, arg2z), {'key4': kwarg4, 'key2': kwarg2}),
}

for n, (args, kwargs) in cases.items():
    print("Test case {:d} ({:#08b}):".format(n,n))
    test_fun(*args, **kwargs)

对于6个布尔值,有2^6个组合,直接对应于从0到2^6-1的数字中的位。如果需要为单个函数调用定义特殊参数,可以通过创建参数列表或字典来解决此问题,然后使用每个参数调用该函数:

for n in range(1<<6):
    if not n in cases:
       raise LookupError('cases do not contain index %d' % n)
cases = {
    0b000000: ((arg1a, arg2y), {'key1': kwarg1, 'key2': kwarg2}),
    0b000001: ((arg1b, arg2x), {'key23': kwarg23, 'key7': kwarg7}),
    # ...
    0b111111: ((arg1a, arg2z), {'key4': kwarg4, 'key2': kwarg2}),
}

for n, (args, kwargs) in cases.items():
    print("Test case {:d} ({:#08b}):".format(n,n))
    test_fun(*args, **kwargs)
如果您不需要关键字参数,那么可以轻松简化

如果需要执行更具体的操作,可以在字典中使用lambda而不是参数集:

cases = {
    0b000000: lambda: test_fun(1,2, foo='bar'),
    0b000001: lambda: test_fun(3,2, moo='bar'),
    # ...
    0b111111: lambda: test_fun(8,3, foo='bar'),
    }

for n, fun in cases.items():
    print("Test case {:d} ({:#08b}):".format(n,n))
    fun()
您还可以将这些词典转换为列表,将dict索引转换为注释:

cases = [
    # 0b000000
    ((arg1a, arg2y), {'key1': kwarg1, 'key2': kwarg2}),
    # 0b000001
    ((arg1b, arg2x), {'key23': kwarg23, 'key7': kwarg7}),
    # ...
    # 0b111111
    ((arg1a, arg2z), {'key4': kwarg4, 'key2': kwarg2}),
]

for n, (args, kwargs) in enumerate(cases):
    print("Test case {:d} ({:#08b}):".format(n,n))
    test_fun(*args, **kwargs)
在所有情况下,我都会确保在案例定义之后直接进行一致性检查,例如

我希望这能给你一些想法

关于CPU使用率,我完全不担心64个不同的测试用例,每个测试用例生成一个PDF文件,这肯定比迭代64个测试用例更需要CPU


真正令人担心的是,您可能会忘记在这个巨大的if-elif-elif构造中列出一个案例,而自己却没有注意到这一点。所以我想用代码让电脑确保每一个组合都被检查过。对于6个布尔参数,我将使用生成器。

int-to-binary方法很聪明,但不能推广。还有一种更简单的方法可以推广到除布尔值以外的数据类型:

import itertools
for i in itertools.product((0, 1,), repeat=3):
    ... stuff ...
这将返回元组,如0,0,0,0,0,1。。。一,一,一


您可以将未命名的第一个参数更改为任何iterable-xrange、set、array等,并将repeat参数更改为所需的长度。适用于任何数据类型

这取决于你要做的事情。在这个问题中,你说你需要画些什么?仅供参考,没有if循环这样的东西。我想你指的是if语句。在这64个案例中到底发生了什么?对于每个条件,它是一段完全独立的代码,还是与布尔变量紧密耦合?感谢您提供的信息stybi,我的目标是在画布上编写。有六行我需要写,比如说,第1行第2行第3行等等,但是如果第2行=0,那么我需要将pdf绘制为第1行第3行等等,这是一个一般规则。不要将布尔值与true或false进行比较。它们本身就是真的或假的,所以如果bool1==true和bool2==false是多余的。这是一个反模式。它浪费空间,需要更多的输入,并且对程序没有任何价值。使用if bool1而不是bool2。非常感谢@maik-s,这一切看起来都非常完美,我迫不及待地要实现并发布我的更新代码,但我是新手,对比特值的概念有点困惑。你能给我解释一下你在那里做了什么吗?我是说你怎么得到3和11。啊,抱歉。当我发布我的答案时,没有看到你的答案。@COLDSPEED无需道歉:等一下,Nevermind@maik-s我现在明白了,非常感谢。天才数字64的二进制表示形式不是0b111111。64是0b1000000。但是,这里只处理从0B0000000到0b111111 63的64个数字,而不是数字64本身。
for n in range(1<<6):
    param1 = 23
    if (n & (1<<0)):
        param1 = 42
    # ...
    param6 = bool(n & (1<<5))

    # ...
    test_fun(param1, ..., param6)
def all_bool_tuples_from_bits(n):
    for k in range(1<<n):
        yield (bool(k&(1<<i)) for i in range(n))

print("Run test_fun with the arguments")
for args in all_bool_tuples_from_bits(6):
    test_fun(*args)

print("Run test_fun with the arguments in explicit variables")
for p0, p1, p2, p3, p4, p5 in all_bool_tuples_from_bits(6):
    test_fun(p0, p1, p2, p3, p4, p5)

print("Run test_fun with the arguments while numbering the test cases")
for n, args in enumerate(all_bool_tuples_from_bits(6)):
    print("Test case {:d} ({:#08b}):".format(n,n))
    test_fun(*args)
import itertools
for i in itertools.product((0, 1,), repeat=3):
    ... stuff ...