2017-05-03 37 views
-2

「1234567890」の代わりにすべての整数を含める方法を教えてもらえますか?その場合、プログラムはトークンができるだけ多くの整数にあるかどうかをチェックします。深さより:すべての整数を含める方法はありますか?

for token in tokenList: 
     if token in "1234567890" 

は、それがすべての整数の代わりに、 "1234567890"

を更新する必要があります。トークンリスト内のトークンは、「私は、言うことができるどのよう 『1234567890:』で トークン場合tokenListでトークン」:

代わりの トークンが整数である場合。

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

あなたはすべての整数の文字列をしたいですか?それらのすべて? – khelwood

+0

[変数が整数か文字列かどうかをチェックする方法は?](http://stackoverflow.com/questions/16488278/how-to-check-if-a-variable-is-an-integer- –

+0

残念ながら、それは重複していません – user7959693

答えて

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

すでに回答されていると思われる場合は、それを重複としてマークしてください。 –

関連する問題