在Python中为任何表达式创建真值表

say*_*786 4 python boolean

我正在尝试创建一个程序,在运行时将询问布尔表达式,变量然后为输入的内容创建一个真值表.我需要使用一个类,这是我到目前为止.我不知道从哪里开始.

from itertools import product

class Boolean(object):

       def __init__(self, statement, vars):
           self.exp = statement
           self.vars = vars

       def __call__(self, statement, vars):

def main():
   expression = raw_input('Give an expression:')
   vars = raw_input('Give names of variables:')
   variables = vars.split(' ')
   b = Boolean(expression, variables)

if __name__ == "__main__":
   main()
Run Code Online (Sandbox Code Playgroud)

pen*_*ant 7

我有一个完全符合您要求的库!退房的GitHub库或发现这里 PyPI上.

自述文件描述了一切如何工作,但这是一个简单的例子:

from truths import Truths
print Truths(['a', 'b', 'x', 'd'], ['(a and b)', 'a and b or x', 'a and (b or x) or d'])
+---+---+---+---+-----------+--------------+---------------------+
| a | b | x | d | (a and b) | a and b or x | a and (b or x) or d |
+---+---+---+---+-----------+--------------+---------------------+
| 0 | 0 | 0 | 0 |     0     |      0       |          0          |
| 0 | 0 | 0 | 1 |     0     |      0       |          1          |
| 0 | 0 | 1 | 0 |     0     |      1       |          0          |
| 0 | 0 | 1 | 1 |     0     |      1       |          1          |
| 0 | 1 | 0 | 0 |     0     |      0       |          0          |
| 0 | 1 | 0 | 1 |     0     |      0       |          1          |
| 0 | 1 | 1 | 0 |     0     |      1       |          0          |
| 0 | 1 | 1 | 1 |     0     |      1       |          1          |
| 1 | 0 | 0 | 0 |     0     |      0       |          0          |
| 1 | 0 | 0 | 1 |     0     |      0       |          1          |
| 1 | 0 | 1 | 0 |     0     |      1       |          1          |
| 1 | 0 | 1 | 1 |     0     |      1       |          1          |
| 1 | 1 | 0 | 0 |     1     |      1       |          1          |
| 1 | 1 | 0 | 1 |     1     |      1       |          1          |
| 1 | 1 | 1 | 0 |     1     |      1       |          1          |
| 1 | 1 | 1 | 1 |     1     |      1       |          1          |
+---+---+---+---+-----------+--------------+---------------------+
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助!


Ome*_*rBA 5

你可以简单地在python中定义任何布尔函数.

考虑以下示例:

def f(w,x,y,z):
    return (x and y) and (w or z)
Run Code Online (Sandbox Code Playgroud)

我写了一个代码片段,它接受任何函数f,并返回它的真值表:

import pandas as pd
from itertools import product

def truth_table(f):
    values = [list(x) + [f(*x)] for x in product([False,True], repeat=f.func_code.co_argcount)]
    return pd.DataFrame(values,columns=(list(f.func_code.co_varnames) + [f.func_name]))
Run Code Online (Sandbox Code Playgroud)

使用它将产生(如果你正在使用IPython Notebook,则使用格式良好的html):

truth_table(f)

    w       x       y       z       f
0   False   False   False   False   False
1   False   False   False   True    False
2   False   False   True    False   False
3   False   False   True    True    False
4   False   True    False   False   False
5   False   True    False   True    False
6   False   True    True    False   False
7   False   True    True    True    True
8   True    False   False   False   False
9   True    False   False   True    False
10  True    False   True    False   False
11  True    False   True    True    False
12  True    True    False   False   False
13  True    True    False   True    False
14  True    True    True    False   True
15  True    True    True    True    True

干杯.

  • 在 Python 3 中,你需要分别使用 `__name__` 和 `__code__` 而不是 `func_name` 和 `func_code` (2认同)

Sha*_*ank 3

您可能想做这样的事情:

from itertools import product
for p in product((True, False), repeat=len(variables)):
    # Map variable in variables to value in p
    # Apply boolean operators to variables that now have values
    # add result of each application to column in truth table
    pass
Run Code Online (Sandbox Code Playgroud)

但 for 循环内部是最难的部分,所以祝你好运。

这是在三个变量的情况下要迭代的示例:

>>> list(product((True, False), repeat=3))
[(True, True, True), (True, True, False), (True, False, True), (True, False, False), (False, True, True), (False, True, False), (False, False, True), (False, False, False)]
Run Code Online (Sandbox Code Playgroud)