Python: determine if a string contains math?

我是研究僧i 提交于 2020-04-07 06:11:44

问题


Given these strings:

"1 + 2"
"apple,pear"

How can I use Python 3(.5) to determine that the first string contains a math problem and nothing else and that the second string does not?


回答1:


Here is a way to do it:

import ast

UNARY_OPS = (ast.UAdd, ast.USub)
BINARY_OPS = (ast.Add, ast.Sub, ast.Mult, ast.Div, ast.Mod)

def is_arithmetic(s):
    def _is_arithmetic(node):
        if isinstance(node, ast.Num):
            return True
        elif isinstance(node, ast.Expression):
            return _is_arithmetic(node.body)
        elif isinstance(node, ast.UnaryOp):
            valid_op = isinstance(node.op, UNARY_OPS)
            return valid_op and _is_arithmetic(node.operand)
        elif isinstance(node, ast.BinOp):
            valid_op = isinstance(node.op, BINARY_OPS)
            return valid_op and _is_arithmetic(node.left) and _is_arithmetic(node.right)
        else:
            raise ValueError('Unsupported type {}'.format(node))

    try:
        return _is_arithmetic(ast.parse(s, mode='eval'))
    except (SyntaxError, ValueError):
        return False



回答2:


Simply use split(), then iterate through the list to check if all instance are either numerical values or operational values. Then use eval.

input = "1 + 2"
for i in input.split():
    if i in ['+','-','*','%','.'] or i.isdigit():
        pass
        # do something
    else:
        pass
        # one element is neither a numerical value or operational value



回答3:


You can use a parsing library such as pyPEG, although there is room for improvment do more than this you could define a grammar like this:

from pypeg2 import optional, List, Namespace
import re

number = re.compile(r'\d+')
binop = re.compile(r'\+|\*') # Exercise: Extend to other binary operators


class BinOp(Namespace):
    grammar = binop


class Number(Namespace):
    grammar = number, optional("."), optional(number)


class Expression(Namespace):
    grammar = Number, optional(BinOp, Number)


class Equation(List):
    grammar = Expression, optional("="), optional(Expression)

You can handle the error when an invalid expression is passed through and use the parse function to validate expressions:

>>> import pypeg2
>>> f = pypeg2.parse("3=3", Equation)
>>> f = pypeg2.parse("3 = 3", Equation)
>>> f = pypeg2.parse("3 + 3 = 3", Equation)
>>> f = pypeg2.parse("3 * 3 = 3", Equation)
>>> f = pypeg2.parse("3hi", Equation)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/site-packages/pypeg2/__init__.py", line 669, in parse
    raise parser.last_error
  File "<string>", line 1
    3hi
     ^
SyntaxError: expecting match on \d+


来源:https://stackoverflow.com/questions/38649496/python-determine-if-a-string-contains-math

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!