我尝试编写一个函数来标记数学表达式,将输入字符串转换为标记列表,但没有成功。有没有一种简单的方法可以在 Python 中做到这一点?例如,给定表达式
sin( 1 + 2 * x ) + tan( 2.123 * x ),
我想获取列表
[ 'sin', '(', '1', '+', '2', '*', 'x', ')', '+', 'tan', '(', '2.123', '*', 'x', ')' ]
Run Code Online (Sandbox Code Playgroud)
提前致谢!
您可以使用tokenize-module。http://docs.python.org/2/library/tokenize.html这是一个例子
>>> 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
Run Code Online (Sandbox Code Playgroud)
还有一种使用正则表达式的方法:
这里是解释正则表达式的链接,这个站点也是测试/探索正则表达式的好工具:http : //regex101.com/r/bP6kH1
>>> s = "sin( 1 + 2 * x ) + tan( 2.123 * x "
>>> import re
>>> re.findall(r"(\b\w*[\.]?\w+\b|[\(\)\+\*\-\/])", s)
['sin', '(', '1', '+', '2', '*', 'x', ')', '+', 'tan', '(', '2.123', '*', 'x']
Run Code Online (Sandbox Code Playgroud)