Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.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
Python 如何简化这些布尔语句_Python_Boolean Logic_Boolean Expression_Boolean Operations - Fatal编程技术网

Python 如何简化这些布尔语句

Python 如何简化这些布尔语句,python,boolean-logic,boolean-expression,boolean-operations,Python,Boolean Logic,Boolean Expression,Boolean Operations,我被告知用python将其压缩为更简化的形式。是否可以有秩序地这样做 (not ((b or not c) and (not a or not c))) or (not (c or not (b and c))) or (a and not c) and (not a or (a and b and c) or (a and ((b and not c) or (not b)))) (not (a and not b) or (not c and b)) and (not b) or (not

我被告知用python将其压缩为更简化的形式。是否可以有秩序地这样做

(not ((b or not c) and (not a or not c))) or (not (c or not (b and c))) or (a and not c) and (not a or (a and b and c) or (a and ((b and not c) or (not b))))

(not (a and not b) or (not c and b)) and (not b) or (not a and b and not c) or (a and not b))

可以使用Symphy计算和简化布尔表达式:

注意:本例中的解析器相当幼稚:

from sympy.logic.boolalg import to_dnf
from sympy.abc import a, b, c

def translate(expr):
    e = list(expr)
    res = [' ' for _ in range(len(e))]
    for start in range(len(e)):
        if expr[start: start+3] == 'not':
            res[start] = '~'
            res[start+1] = ''
            res[start+2] = ''
        elif expr[start: start+3] == 'and':
            res[start] = '&'
            res[start+1] = ''
            res[start+2] = ''
        else:
            if res[start] == ' ':
                res[start] = e[start]

    expr = ''.join(res)         
    e = list(expr)
    res = [' ' for _ in range(len(e))]
    for start in range(len(e)):
        if expr[start: start+2] == 'or':
            res[start] = '|'
            res[start+1] = ''
        else:
            if res[start] == ' ':
                res[start] = e[start]

    res = [elt for elt in res if elt != ' ' or elt != '']
    return ''.join(res)


exp1 = '(not ((b or not c) and (not a or not c))) or (not (c or not (b and c))) or (a and not c) and (not a or (a and b and c) or (a and ((b and not c) or (not b))))'
exp2 = '(not (a and not b) or (not c and b)) and (not b) or (not a and b and not c) or (a and not b)'


print('exp1:', '\n', eval(translate(exp1)), '\n', to_dnf(eval(translate(exp1)), simplify=True))
print('exp2:', '\n', eval(translate(exp2)), '\n', to_dnf(eval(translate(exp2)), simplify=True))
输出简化表达式 正确性验证: 对于每个输入,计算原始表达式和简化表达式的真值表

def evaluate(a, b, c):
    exp1 = (not ((b or not c) and (not a or not c))) or (not (c or not (b and c))) or (a and not c) and (not a or (a and b and c) or (a and ((b and not c) or (not b))))
    exp2 = (not (a and not b) or (not c and b)) and (not b) or (not a and b and not c) or (a and not b)
    return exp1, exp2

def evaluate2(a, b, c):
    exp1 = a or (c and not b)
    exp2 = not b or (not a and not c)
    return exp1, exp2

values = [(1, 1, 1), (1, 1, 0), (1, 0, 1), (0, 1, 1), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)]
for vals in values:
    print(f'{vals}: {evaluate(*vals)}, {evaluate2(*vals)}')
真值表输出:
@User2357112Karnaugh映射很好,但是源代码不是太复杂,无法填充单元格吗?不太确定这个问题与Python有什么关系。在任何使用布尔运算符的语言中都需要执行相同的过程。而且
def evaluate(a, b, c):
    exp1 = (not ((b or not c) and (not a or not c))) or (not (c or not (b and c))) or (a and not c) and (not a or (a and b and c) or (a and ((b and not c) or (not b))))
    exp2 = (not (a and not b) or (not c and b)) and (not b) or (not a and b and not c) or (a and not b)
    return exp1, exp2

def evaluate2(a, b, c):
    exp1 = a or (c and not b)
    exp2 = not b or (not a and not c)
    return exp1, exp2

values = [(1, 1, 1), (1, 1, 0), (1, 0, 1), (0, 1, 1), (1, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)]
for vals in values:
    print(f'{vals}: {evaluate(*vals)}, {evaluate2(*vals)}')
(1, 1, 1): (True, False), (1, False)
(1, 1, 0): (True, False), (1, False)
(1, 0, 1): (True, True), (1, True)
(0, 1, 1): (0, 0), (False, False)
(1, 0, 0): (True, True), (1, True)
(0, 1, 0): (0, True), (0, True)
(0, 0, 1): (True, True), (True, True)
(0, 0, 0): (0, True), (0, True)