python 数学表达式评估
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1545403/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Math Expression Evaluation
提问by Ori Shavit
What is the best way to implement a python program that will take a string and will output its result according to operator precedence (for example: "4+3*5" will output 19). I've googled for ways to solve this problem but they were all too complex, and I'm looking for a (relatively) simple one.
实现一个接受字符串并根据运算符优先级输出其结果的python程序的最佳方法是什么(例如:“4+3*5”将输出19)。我在谷歌上搜索了解决这个问题的方法,但它们都太复杂了,我正在寻找一个(相对)简单的方法。
clarification: I need something slightly more advanced than eval() - I want to be able to add other operators (for example a maximum operator - 4$2 = 4) or, also I am more interested in this academically than professionaly - I want to know howto do this.
澄清:我需要比 eval() 更高级的东西 - 我希望能够添加其他运算符(例如最大运算符 - 4$2 = 4),或者,我对学术比专业更感兴趣 - 我想知道如何做到这一点。
回答by Martin B
If you're "academically interested", you want to learn about how to write a parser with operator precedence.
如果您“对学术感兴趣”,您想了解如何编写具有运算符优先级的解析器。
Simple Top-Down Parsing in Pythonis a nice article that builds an example parser to do exactly what you want to do: Evaluate mathematical expressions.
Python中的Simple Top-Down Parsing是一篇不错的文章,它构建了一个示例解析器来完成您想做的事情:评估数学表达式。
I can highly recommend having a go at writing your own first parser -- it's one of those "ah, that'show that works" moments!
我强烈建议您尝试编写自己的第一个解析器——这是“啊,这就是它的工作原理”时刻之一!
回答by Lennart Regebro
That's what the "eval" function does in Python.
这就是 Python 中的“eval”函数所做的。
result = eval(expression)
Beware though it can do a whole lot more, primarily call functions, so to be safe you should make sure it can't access the locals or globals. Also, you can access the builtin methods, including the tricky importso you need to block access to that as well:
请注意,尽管它可以做更多的事情,主要是调用函数,但为了安全起见,您应该确保它不能访问局部变量或全局变量。此外,您可以访问内置方法,包括棘手的导入,因此您还需要阻止访问:
result = eval(expression, {'__builtins__': None}, {})
But that's only if you need security, that is if you allow anybody to type in any expression.
但这只是在您需要安全性的情况下,也就是说,如果您允许任何人输入任何表达式。
Of course since you this way block all locla variables from use, then you don't have any variables to use, so to have that you need to pass in just those variables which should be accessed in the dictionaries.
当然,由于您以这种方式阻止了所有 locla 变量的使用,因此您没有任何变量可以使用,因此您只需要传入那些应该在字典中访问的变量。
vars = {'__builtins__': None, 'x': x}
result = eval(expression, vars, {})
or similar.
或类似。
回答by Kathy Van Stone
回答by Thomas Owens
I'm not terribly familiar with Python and any extremely Pythonic methods, but you could look at the Interpreter pattern, which is defined in the Gang of Four book. It's designed for processing a "language", and mathematical expressions do follow a particular language with rules. In fact, the example on Wikipedia is actually a Java implementation of a RPN calculator.
我不太熟悉 Python 和任何非常 Python 化的方法,但您可以查看解释器模式,它在四人组书中定义。它是为处理“语言”而设计的,数学表达式确实遵循具有规则的特定语言。其实维基百科上的例子其实就是一个RPN计算器的Java实现。
回答by Kaouni
The java alternative is here http://code.google.com/p/expressionoasis/
Java 替代方法在这里http://code.google.com/p/expressionoasis/
回答by Philippe F
This receipe gives the proper answer to your problem:
这份收据为您的问题提供了正确的答案:
http://code.activestate.com/recipes/496746-restricted-safe-eval/
http://code.activestate.com/recipes/496746-restricted-safe-eval/
It allows you to eval limited statement that can not harm your computer or your program.
它允许您评估不会损害您的计算机或程序的有限语句。