[英]How to store and read my logical operation on python
I am making a program to filter my data using some parameters and logical operation.我正在制作一个程序来使用一些参数和逻辑运算过滤我的数据。
I have a lot of classrooms data that has its characteristics, so each classroom will have a different filter.我有很多教室数据都有它的特点,所以每个教室都会有不同的过滤器。
if classrooms == 1:
if data[A] > data[B] & data[C] != data [D]:
print("matched")
elif classrooms == 2:
if data[A] < data[B] & data[C] == data [D]:
print("matched")
elif classrooms == 3:
if data[B] < data[D] & data[A] == data [C]:
print("matched")
...
...
elif classrooms == 5000:
if data[R] < data[A] & data[W] == data [H]:
print("matched")
since the operator is similar, is there any method to read my logical filter from my stored file to the python program?由于运算符类似,是否有任何方法可以将我的逻辑过滤器从我存储的文件中读取到 python 程序?
"(A<B)&(C!=D)"
"(A>B)&(C==D)"
..
..
"(R<A)&(W==H)"
So, I don't have to write all my logical filters for each classroom in python that causing a huge line in python.所以,我不必在 python 中为每个教室编写所有逻辑过滤器,这会导致 python 中的一行。 I just read from my stored text data, and my python program will interpret
我只是从我存储的文本数据中读取,我的 python 程序将解释
"(A<B)&(C!=D)"
to this program到这个程序
if data[A] > data[B] & data[C] != data [D]:
You could parse the filters in your file using a regular expression , and then construct a chain of functions from the operator module to execute the filter.您可以使用正则表达式解析文件中的过滤器,然后从 operator 模块构造函数链来执行过滤器。
This expression这个表情
import re
rx = re.compile(r"""^ # Beginning of string
\( # Opening outer parenthesis
(?P<operand0>[A-Z]) # First operand
(?P<operator0>[^A-Z]+) # First operator
(?P<operand1>[A-Z]) # Second operand
\) # Closing outer parenthesis
(?P<operator1>[^A-Z]+) # Second operator
\( # Opening oute parenthesis
(?P<operand2>[A-Z]) # Third operand
(?P<operator2>[^A-Z]+) # Third operator
(?P<operand3>[A-Z]) # Fourth operand
\) # Closing outer parenthesis
$ # End of string
""",
re.VERBOSE)
matches the structure of your filters.匹配您的过滤器的结构。
They can be matched like this:它们可以这样匹配:
m = rx.match("(A<B)&(C!=D)")
Parts of the filter may be accessed using the names assigned inside the (?P<name>)
sub-expressions可以使用
(?P<name>)
子表达式中分配的名称访问过滤器的某些部分
m['operand0']
'A'
m['operator0']
'<'
Using a dictionary to map operators to functions from the operator module使用字典将运算符映射到运算符模块中的函数
import operator
lookup = {
'<': operator.lt,
'&': operator.and_,
'!=': operator.ne,
}
you can build an expression and evaluate it您可以构建一个表达式并对其进行评估
op0 = lookup[m['operator0']]
op1 = lookup[m['operator1']]
op2 = lookup[m['operator2']]
result = op1(op0(a, b), op2(c, d))
How you derive a, b, c, d
from the operands is something you will need to work out.如何从操作数中导出
a, b, c, d
是您需要解决的问题。
The regular expression above assumes operands are single uppercase characters and operators are one or more non-uppercase characters.上面的正则表达式假定操作数是单个大写字符,运算符是一个或多个非大写字符。 This can made more precise, for example you could use the regex alternation operator
|
这可以更精确,例如您可以使用正则表达式交替运算符
|
to make an expression that matches only the operators that are used制作一个仅匹配使用的运算符的表达式
r'&|<|>|!=|=='
instead of the overly-general而不是过于笼统
r'[^A-Z]+'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.