Python:确定一个字符串是否包含数学?

时间:2016-07-29 02:12:49

标签: python python-3.x math python-3.5

鉴于这些字符串:

file.copy()

如何使用Python 3(.5)确定第一个字符串包含数学问题而没有别的并且第二个字符串不包含?

4 个答案:

答案 0 :(得分:7)

这是一种方法:

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

答案 1 :(得分:0)

只需使用split(),然后遍历列表以检查所有实例是数值还是操作值。然后使用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

答案 2 :(得分:0)

您可以使用解析库,例如pyPEG,虽然有改进的余地,但您可以定义这样的语法:

>>> 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+

您可以在传递无效表达式时处理错误,并使用parse函数验证表达式:

var gzipcompresseddata=new Int8Array([120,156,237,87,93,111,155,48,20,125,110,165,254,7,228,31,16,62,178,180,123,0,36,66,12,65,50,4,129,163,173,79,22,16,7,101,3,202,40,168,233,191,159,13,88,165,157,52,181,219,67,165,8,158,238,185,199,215,220,175,35,132,30,219,145,23,98,243,230,250,74,247,119,27,136,188,192,217,113,36,224,198,212,180,187,149,46,11,212,83,248,62,132,156,209,229,209,234,189,54,178,226,152,1,245,43,59,47,64,207,184,187,221,134,3,69,151,133,217,251,99,124,143,120,248,237,82,151,133,205,51,145,95,165,162,251,22,134,145,103,33,169,74,74,106,0,76,207,100,29,18,167,107,136,77,124,47,112,29,24,184,196,141,241,119,226,107,43,85,65,27,151,40,96,204,105,143,144,153,38,217,79,150,17,55,123,231,6,121,110,128,77,90,37,105,65,117,121,132,3,5,67,188,117,246,129,45,29,187,42,51,64,65,127,117,73,1,228,9,251,45,242,48,52,219,166,227,161,47,142,225,128,231,56,251,24,74,141,1,148,133,210,63,64,202,167,32,157,128,241,214,56,132,44,55,43,250,88,148,229,175,61,24,224,15,190,106,235,5,1,140,99,83,16,172,239,194,53,45,128,248,86,216,59,174,116,24,88,107,36,234,29,193,192,216,91,139,69,34,62,85,97,14,4,11,150,218,231,154,141,74,59,0,233,169,73,106,3,52,180,166,73,11,164,242,84,151,28,243,251,128,116,60,21,45,101,21,176,89,67,43,98,195,12,89,44,25,16,144,186,166,48,0,48,91,122,110,187,134,62,202,79,15,15,7,162,104,170,182,248,81,231,108,73,198,28,217,24,222,100,173,179,137,110,49,219,140,119,213,129,246,108,137,172,192,30,199,166,174,38,189,20,32,157,130,196,0,234,226,118,218,216,151,102,168,239,105,70,86,36,101,253,247,94,252,89,125,90,147,35,91,249,140,148,167,42,63,210,42,39,249,99,123,38,37,95,249,226,144,147,98,81,87,175,186,242,182,9,156,27,132,244,175,170,82,103,85,93,174,170,150,179,170,62,71,85,218,172,170,203,83,85,125,202,184,65,84,77,153,133,245,73,194,90,206,194,186,60,97,245,159,43,85,253,114,55,171,234,191,85,197,6,47,126,255,110,174,127,3,92,166,143,255])

var blob=new Blob([gzipcompresseddata],{type:"application/zip"})

答案 3 :(得分:0)

import re

input = "1 + 2"

if re.match(r"[\w\s]*[\d\+\-\*\/]+[\w\s]*", input):
    # do whatever you want
    math = re.findall(r"([\d\+\-\*\/]+)", user_input_without_syntax)[0] # if you need that