Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python标记数学表达式_Python_Math - Fatal编程技术网

用Python标记数学表达式

用Python标记数学表达式,python,math,Python,Math,我试图编写一个函数来标记一个数学表达式,将输入字符串转换成一个标记列表,但没有成功。在Python中有没有一种简单的方法可以做到这一点?例如,给定表达式 sin(1+2*x)+tan(2.123*x) 我想得到这份名单 [ 'sin', '(', '1', '+', '2', '*', 'x', ')', '+', 'tan', '(', '2.123', '*', 'x', ')' ] 提前谢谢 您可以使用标记化-模块。这里有一个例子 >>> s = "sin( 1 + 2

我试图编写一个函数来标记一个数学表达式,将输入字符串转换成一个标记列表,但没有成功。在Python中有没有一种简单的方法可以做到这一点?例如,给定表达式

sin(1+2*x)+tan(2.123*x)

我想得到这份名单

[ 'sin', '(', '1', '+', '2', '*', 'x', ')', '+', 'tan', '(', '2.123', '*', 'x', ')' ]

提前谢谢

您可以使用
标记化
-模块。这里有一个例子

>>> s = "sin( 1 + 2 * x ) + tan( 2.123 * x "
>>> import tokenize
>>> from StringIO import StringIO
>>> tokenize.tokenize(StringIO(s).readline)
1,0-1,3:    NAME    'sin'
1,3-1,4:    OP  '('
1,5-1,6:    NUMBER  '1'
1,7-1,8:    OP  '+'
1,9-1,10:   NUMBER  '2'
1,11-1,12:  OP  '*'
1,13-1,14:  NAME    'x'
1,15-1,16:  OP  ')'
1,17-1,18:  OP  '+'
1,19-1,22:  NAME    'tan'
1,22-1,23:  OP  '('
1,24-1,29:  NUMBER  '2.123'
1,30-1,31:  OP  '*'
1,32-1,33:  NAME    'x'
# and now occurs some error you have to catch
还有一种使用正则表达式的方法:

以下是解释regex的链接,该网站也是测试/探索regex的绝佳工具:

您可以使用解析此类型的表达式:

from pyparsing import *

expr = Forward()

double = Word(nums + ".").setParseAction(lambda t:float(t[0]))
integer = Word(nums).setParseAction(lambda t:int(t[0]))
variable = Word(alphas)
string = dblQuotedString
funccall = Group(variable + "(" + Group(Optional(delimitedList(expr))) + ")")
array_func = Group(funccall + "[" + Group(delimitedList(expr, "][")) + "]")
array_var = Group(variable + "[" + Group(delimitedList(expr, "][")) + "]")

operand = double | string | array_func | funccall | array_var | variable

expop = Literal('^')
signop = oneOf('+ -')
multop = oneOf('* /')
plusop = oneOf('+ -')

expr << operatorPrecedence( operand,
[("^", 2, opAssoc.RIGHT),
(signop, 1, opAssoc.RIGHT),
(multop, 2, opAssoc.LEFT),
(plusop, 2, opAssoc.LEFT),]
)

result = expr.parseString('sin( 1 + 2 * x ) + tan( 2.123 * x )')
print result
它是一个嵌套列表,允许遵守运算符优先级。要获得所需的平面列表,只需将列表展平:

import collections

def flatten(l):
    for el in l:
        if isinstance(el, collections.Iterable) and not isinstance(el, basestring):
            for sub in flatten(el):
                yield sub
        else:
            yield el

print list(flatten(result))
印刷品:

[[['sin', '(', [[1.0, '+', [2.0, '*', 'x']]], ')'], '+', ['tan', '(', [[2.123, '*', 'x']], ')']]]
['sin', '(', 1.0, '+', 2.0, '*', 'x', ')', '+', 'tan', '(', 2.123, '*', 'x', ')']
或者,如果您只想标记化,而不考虑运算符优先级或结构,您可以在一行中完成:

>>> from pyparsing import *
>>> OneOrMore(Word(alphas+"_", alphanums+"_") | Word(printables)).parseString("sin( 1 + 2 * x ) + tan( 2.123 * x )").asList()
['sin', '(', '1', '+', '2', '*', 'x', ')', '+', 'tan', '(', '2.123', '*', 'x', ')']

你有没有尝试过什么,或者你希望我们能为你编写它?谷歌搜索“用python编写lexer”会产生很多相关的结果。我建议你先去探索一下。就像我说的,我试过了。谢谢你的帮助,NPE!我删除了代码,不过还是要谢谢你。
>>> from pyparsing import *
>>> OneOrMore(Word(alphas+"_", alphanums+"_") | Word(printables)).parseString("sin( 1 + 2 * x ) + tan( 2.123 * x )").asList()
['sin', '(', '1', '+', '2', '*', 'x', ')', '+', 'tan', '(', '2.123', '*', 'x', ')']