有人可以告诉我如何包含所有整数而不仅仅是#34; 1234567890"。因此,在这种情况下,程序将检查令牌是否尽可能多的整数。
for token in tokenList:
if token in "1234567890"
更新:更深入。
而不是"用于tokenList中的令牌: 如果令牌在" 1234567890" 我怎么说,#34;令牌列表中的令牌: 如果令牌是整数。
def infixToPostfix(infixexpr):
prec = {}
prec["**"] = 3
prec["*"] = 3
prec["/"] = 3
prec["+"] = 2
prec["-"] = 2
prec["("] = 1
prec[")"] = 1
opStack = Stack()
postfixList = []
tokenList = infixexpr.split()
for token in tokenList:
if token in "1234567890":
postfixList.append(token)
elif token == '(':
opStack.push(token)
elif token == ')':
topToken = opStack.pop()
while topToken != '(':
postfixList.append(topToken)
topToken = opStack.pop()
else:
while (not opStack.isEmpty()) and \
(prec[opStack.peek()] >= prec[token]):
postfixList.append(opStack.pop())
opStack.push(token)
while not opStack.isEmpty():
postfixList.append(opStack.pop())
return " ".join(postfixList)
答案 0 :(得分:0)
已经解决:How to check if a variable is an integer or a string?
try: value = int(value) except ValueError: pass # it was a string, not an int. (or anything alse)