Python: determine if a string contains math?

2020-04-23 06:48发布

问题:

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+