例如,我需要 listBuilder('24+3-65*2')
返回 ['24', '+', '3', '-', '65', '*', '2']
字符串列出与整数分组
我们不允许使用自定义的导入函数。我必须在没有他们的情况下做这项工作。这是我迄今为止...
def listBuilder(expr):
operators = ['+', '-', '*', '/', '^']
result = []
temp = []
for i in range(len(expr)):
if expr[i] in operators:
result.append(expr[i])
elif isNumber(expr[i]): #isNumber() returns true if the string can convert to float
temp += expr[i]
if expr[i+1] in operators:
tempTwo = ''.join(temp)
result.append(tempTwo)
temp = []
tempTwo = []
elif expr[i+1] == None:
break
else:
continue
return result
在这一点上,我得到一个错误,串索引超出范围包括expr[i+1]
行。帮助将不胜感激。我一直坚持了几个小时。
也许重复的问题https://stackoverflow.com/questions/47616114/how-to-loop-over-the-elementary-arithmetic-symbols – dkato
你是如何处理的负数? – RoadRunner