例如,我需要 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