两个字典之间的布尔表达式

2024-04-27 04:18:27 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我在这里的第一篇文章,我希望我的问题是明确的,格式正确。。。你知道吗

我有两本字典。第一个字典包含我称之为“选项表达式”的键和值的任意项:

dict1 = {'((opt1 OR opt4 OR opt6) AND NOT opt7)': 'Yellow Roof', '((opt2 AND opt3) XOR opt5': 'Purple Scooter'}

print(dict1)

{'((opt1 OR opt4 OR opt6) AND NOT opt7)': 'Yellow Roof',
'((opt2 AND opt3) XOR opt5': 'Purple Scooter'}

第二个字典包含来自dict1的选项作为键,以及它们是否作为值“接受”或“拒绝”:

dict2 = {'opt1': 'Accepted', 'opt2': 'Rejected', 'opt3': 'Rejected','opt4': 'Accepted', 'opt5': 'Accepted', 'opt6': 'Rejected','opt7': 'Accepted'}

print(dict2)

{'opt1': 'Accepted',
 'opt2': 'Rejected',
 'opt3': 'Rejected',
 'opt4': 'Accepted',
 'opt5': 'Accepted',
 'opt6': 'Rejected',
 'opt7': 'Accepted'}

我将如何评估来自dict1的表达式作为TrueFalse来根据来自dict2的opt是被接受还是被拒绝来确定选择了哪些项?你知道吗

我最初的想法是将表达式中的opt替换为1表示accepted,0表示rejected,这样可以得到如下结果:

((1 OR 1 OR 0) AND NOT 1)其计算结果为False

以及

((0 AND 0) XOR 1)将作为True计算

我对pyparsing做了一些研究,认为它在这里会很有用,尽管我不完全确定如何最好地利用它。你知道吗

我正在应用布尔逻辑:

并且:当且仅当双方都是真的时才是真的

或:如果任何一方为真,则为真

NOT/(AND NOT):由真变假,由假变真

XOR:如果一方为真,则为真(但是 两者)


Tags: orand字典notxorrejecteddict1accepted
1条回答
网友
1楼 · 发布于 2024-04-27 04:18:27

我想出了一个解决办法。它假设您已经用True和False替换了“Accepted”和“Rejected”(或您在问题中所说的1和0)

然后将处理结果。它首先将结果格式化为有效的Python代码('xor'->;^'和'->;and和'OR'->;or),用布尔值替换opt值,最后使用eval内置函数计算表达式。你知道吗

input_code = '((opt1 OR opt4 OR opt6) AND NOT opt7)'
input_w_xor = '(opt2 AND opt3) XOR opt5'
result_codes = {'opt1': True, 'opt2': False, 'opt3': False,'opt4': True, 'opt5': True, 'opt6': False,'opt7': True}


def process_code(input_code, result_codes):
    input_code = input_code.lower()

    def replace_op_codes(code):
        for opt_key, opt_value in result_codes.items():
            code =  code.replace(opt_key, str(opt_value))
        return code

    input_code = replace_op_codes(input_code)
    input_code = input_code.replace("xor", '^')
    result = eval(input_code)

    return result


print(process_code(input_code, result_codes))
print(process_code(input_w_xor, result_codes))

我倾向于回避使用evalexec,但我认为这是这种情况下最快的解决方案。你知道吗

网友
2楼 · 发布于 2024-04-27 04:18:27

只需对数据稍加处理,就可以将其转换为有效的Python,并让Python解释器执行您的命令:

# Mandatory warning here about using eval() - here be dragons!

dict1 = {"((opt1 OR opt4 OR opt6) AND NOT opt7)": "Yellow Roof",
         "((opt2 AND opt3) XOR opt5)": "Purple Scooter"}

dict2 = {"opt1": "Accepted",
         "opt2": "Rejected",
         "opt3": "Rejected",
         "opt4": "Accepted",
         "opt5": "Accepted",
         "opt6": "Rejected",
         "opt7": "Accepted"}

# lets first normalize the 'opt' codes to True/False
dict2_norm = {k: (True if v == "Accepted" else False) for k, v in dict2.items()}

# Now all we need to do is evaluate the expressions:
for expression, value in dict1.items():
    # Let's first normalize the expression to Python-digestible
    expression = expression.replace("XOR", "is not").replace("OR", "or")\
        .replace("AND", "and").replace("NOT", "not")
    if eval(expression, dict2_norm):
        print(value)

# prints: Purple Scooter

相关问题 更多 >