【问题标题】:Is there any way I can include all integers?有什么办法可以包含所有整数?
【发布时间】:2017-10-01 21:02:19
【问题描述】:

谁能告诉我如何包含所有整数而不是“1234567890”。因此,在这种情况下,程序将检查令牌是否为尽可能多的整数。

for token in tokenList:
        if token in "1234567890" 

需要全部为整数而不是“1234567890”

更新:更深入。

而不是“for tokenList 中的令牌: 如果令牌在“1234567890”中 我怎么说,“对于令牌列表中的令牌: 如果令牌是整数。

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)

【问题讨论】:

标签: python python-2.7 python-3.x wxpython ipython


【解决方案1】:

已经解决了: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)

【讨论】:

  • 如果您认为它已被回答,请将其标记为重复。
猜你喜欢
  • 2014-05-22
  • 2020-05-08
  • 1970-01-01
  • 2022-01-27
  • 1970-01-01
  • 2020-02-05
  • 1970-01-01
  • 2021-06-26
  • 1970-01-01
相关资源
最近更新 更多