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

运算符和操作数之间可能有空格。运算符和操作数都必须作为列表中的字符串返回。操作数必须是浮点数。

例如。

输入:1+2 *3/(4+5)

输出:['1.00', '+', '2.00', '*'......]

我不确定出了什么问题。如果你教我,我将不胜感激。

4

1 回答 1

0

我试着用正则表达式来做,看看:

import re

input = "1+2 *3  (4+5)"
input = input.replace(' ','')

print([i for i in re.split('([^0-9])',input) if i != ''])

希望能帮助到你!

于 2020-04-09T19:59:47.067 回答