[英]Function to split the input expression into token units(python)
def get_token_list(expr):
token_list_initial = expr.split(' ')
token_list=[]
for token in token_list_initial:
if token in '+-/*^()':
token_list.append(token)
elif token == ' ':
continue
elif token in '0123456789':
token=float(token)
token=str(token)
token_list.append(token)
return token_list
There may be a space between the operator and the operand.运算符和操作数之间可能有空格。 Both operators and operands must be returned as strings in a list.运算符和操作数都必须作为列表中的字符串返回。 Operand must be float.操作数必须是浮点数。
Eg.例如。
input: 1+2 *3/(4+5)
输入: 1+2 *3/(4+5)
output: ['1.00', '+', '2.00', '*'......]
output: ['1.00', '+', '2.00', '*'......]
I am not sure what is wrong.我不确定出了什么问题。 I would really appreciate it if you taught me.如果你教我,我将不胜感激。
I tried to do it using regex, take a look:我试着用正则表达式来做,看看:
import re
input = "1+2 *3 (4+5)"
input = input.replace(' ','')
print([i for i in re.split('([^0-9])',input) if i != ''])
Hope it helps!希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.