堆栈实验 [英] Stack experiment

查看:91
本文介绍了堆栈实验的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!我是Python新手,从互联网上做运动。应该使用

Stack()类来评估使用postfix运算符给出的表达式。

类Stack:

def __init __(自我):

self.items = []


def push(self,item):

self.items.append(item)


def pop(self):

返回self.items.pop()


def isEmpty(self):

return(self.items == [])

def evaluatePostfix(expr):

导入re

tokenList = re.split("([^ 0-9])",expr)

stack = Stack( )令牌列表中令牌的


如果令牌==''''或令牌=='''':

继续

如果令牌==''+'':

sum = stack.pop()+ stack.pop()

stack.push(总和)

elif token ==''*'':

product = stack.pop()* stack.pop()

stack。推(产品)

否则:

stack.push(int(令牌))

返回stack.pop()


打印evaluatePostfix(56 47 + 2 *)


错误:

回溯(最近一次调用最后一次) :

文件C:\ * \postfix1.py,第31行,< module>

print evaluatePostfix(" 56 47 + 2 * ")

文件" C:\ * \postfix1.py",第28行,在evaluatePostfix中

stack.push(int(token))

ValueError:基数为10的int()的文字无效:''56 47''


如何避免错误并获得所需的结果?

Hi! Im new to Python and doing exercise found from internet. It is
supposed to evaluate expression given with postfix operator using
Stack() class.

class Stack:
def __init__(self):
self.items = []

def push(self, item):
self.items.append(item)

def pop(self):
return self.items.pop()

def isEmpty(self):
return (self.items == [])

def evaluatePostfix(expr):
import re
tokenList = re.split(" ([^0-9])", expr)
stack = Stack()
for token in tokenList:
if token == '''' or token == '' '':
continue
if token == ''+'':
sum = stack.pop() + stack.pop()
stack.push(sum)
elif token == ''*'':
product = stack.pop() * stack.pop()
stack.push(product)
else:
stack.push(int(token))
return stack.pop()

print evaluatePostfix("56 47 + 2 *")

Errormsg:
Traceback (most recent call last):
File "C:\*\postfix1.py", line 31, in <module>
print evaluatePostfix("56 47 + 2 *")
File "C:\*\postfix1.py", line 28, in evaluatePostfix
stack.push(int(token))
ValueError: invalid literal for int() with base 10: ''56 47''

How can I avoid the error and get desired result?

推荐答案


to *@finland.com 写道:

嗨!我是Python新手,从互联网上做运动。应该使用

Stack()类来评估使用postfix运算符给出的表达式。

类Stack:

def __init __(自我):

self.items = []


def push(self,item):

self.items.append(item)


def pop(self):

返回self.items.pop()


def isEmpty(self):

return(self.items == [])

def evaluatePostfix(expr):

导入re

tokenList = re.split("([^ 0-9])",expr)

stack = Stack( )令牌列表中令牌的


如果令牌==''''或令牌=='''':

继续

如果令牌==''+'':

sum = stack.pop()+ stack.pop()

stack.push(总和)

elif token ==''*'':

prod uct = stack.pop()* stack.pop()

stack.push(product)

else:

stack.push(int (令牌))

返回stack.pop()


打印evaluatePostfix(" 56 47 + 2 *")

>
错误:

回溯(最近一次调用最后一次):

文件C:\ * \postfix1.py,第31行, < module>

print evaluatePostfix(" 56 47 + 2 *")

文件" C:\ * \postfix1.py",第28行,在evaluatePostfix中

stack.push(int(token))

ValueError:基数为10的int()的文字无效:''56 47''


如何避免错误并获得理想的结果?
Hi! Im new to Python and doing exercise found from internet. It is
supposed to evaluate expression given with postfix operator using
Stack() class.

class Stack:
def __init__(self):
self.items = []

def push(self, item):
self.items.append(item)

def pop(self):
return self.items.pop()

def isEmpty(self):
return (self.items == [])

def evaluatePostfix(expr):
import re
tokenList = re.split(" ([^0-9])", expr)
stack = Stack()
for token in tokenList:
if token == '''' or token == '' '':
continue
if token == ''+'':
sum = stack.pop() + stack.pop()
stack.push(sum)
elif token == ''*'':
product = stack.pop() * stack.pop()
stack.push(product)
else:
stack.push(int(token))
return stack.pop()

print evaluatePostfix("56 47 + 2 *")

Errormsg:
Traceback (most recent call last):
File "C:\*\postfix1.py", line 31, in <module>
print evaluatePostfix("56 47 + 2 *")
File "C:\*\postfix1.py", line 28, in evaluatePostfix
stack.push(int(token))
ValueError: invalid literal for int() with base 10: ''56 47''

How can I avoid the error and get desired result?



显然你的令牌是错误的,因为其中一个令牌是56 47,而错误信息表示为




import re

print re.split("([^ 0-9])"," 56 47 + 2 *")


看起来你只想要expr.split()。

Obviously your tokens are wrong since one of the tokens is ''56 47'' as
the error message indicates.

import re
print re.split(" ([^0-9])", "56 47 + 2 *")

It looks like you''d just want expr.split().


4月3日,10:57 am,t ... @ finland.com写道:
On Apr 3, 10:57 am, t...@finland.com wrote:

嗨!我是Python新手,从互联网上做运动。应该使用

Stack()类来评估使用postfix运算符给出的表达式。

类Stack:

def __init __(自我):

self.items = []


def push(self,item):

self.items.append(item)


def pop(self):

返回self.items.pop()


def isEmpty(self):

return(self.items == [])

def evaluatePostfix(expr):

导入re

tokenList = re.split("([^ 0-9])",expr)

stack = Stack( )令牌列表中令牌的


如果令牌==''''或令牌=='''':

继续

如果令牌==''+'':

sum = stack.pop()+ stack.pop()

stack.push(总和)

elif token ==''*'':

prod uct = stack.pop()* stack.pop()

stack.push(product)

else:

stack.push(int (令牌))

返回stack.pop()


打印evaluatePostfix(" 56 47 + 2 *")

>
错误:

回溯(最近一次调用最后一次):

文件C:\ * \postfix1.py,第31行, < module>

print evaluatePostfix(" 56 47 + 2 *")

文件" C:\ * \postfix1.py",第28行,在evaluatePostfix中

stack.push(int(token))

ValueError:基数为10的int()的文字无效:''56 47''


如何避免错误并获得理想的结果?
Hi! Im new to Python and doing exercise found from internet. It is
supposed to evaluate expression given with postfix operator using
Stack() class.

class Stack:
def __init__(self):
self.items = []

def push(self, item):
self.items.append(item)

def pop(self):
return self.items.pop()

def isEmpty(self):
return (self.items == [])

def evaluatePostfix(expr):
import re
tokenList = re.split(" ([^0-9])", expr)
stack = Stack()
for token in tokenList:
if token == '''' or token == '' '':
continue
if token == ''+'':
sum = stack.pop() + stack.pop()
stack.push(sum)
elif token == ''*'':
product = stack.pop() * stack.pop()
stack.push(product)
else:
stack.push(int(token))
return stack.pop()

print evaluatePostfix("56 47 + 2 *")

Errormsg:
Traceback (most recent call last):
File "C:\*\postfix1.py", line 31, in <module>
print evaluatePostfix("56 47 + 2 *")
File "C:\*\postfix1.py", line 28, in evaluatePostfix
stack.push(int(token))
ValueError: invalid literal for int() with base 10: ''56 47''

How can I avoid the error and get desired result?



我不知道你为什么使用重新模块。为此,我会跳过

它。更改您的代码,使其如下所示:

def evaluatePostfix(expr):

tokenList = expr.split("")

stack = Stack()
用于令牌列表中令牌的


如果令牌==''''或令牌=='''':

继续

如果令牌==''+'':

sum = stack.pop()+ stack.pop()

stack.push(sum)

elif token ==''*'':

product = stack.pop()* stack.pop()

stack.push(产品)

else:

stack.push(int(token))

返回堆栈.pop()


#这对我有用。 重新可能有问题。代码

在你的例子中,但我不太了解那个

区域。


Mike

I don''t know why you''re using the "re" module. For this, I would skip
it. Change your code so that it looks like this:

def evaluatePostfix(expr):
tokenList = expr.split(" ")
stack = Stack()
for token in tokenList:
if token == '''' or token == '' '':
continue
if token == ''+'':
sum = stack.pop() + stack.pop()
stack.push(sum)
elif token == ''*'':
product = stack.pop() * stack.pop()
stack.push(product)
else:
stack.push(int(token))
return stack.pop()

# this worked for me. There may be something wrong with the "re" code
in your example, but I don''t know enough about that to help in that
area.

Mike




< ky ****** @ gmail.comwrote in message

<ky******@gmail.comwrote in message

>re可能有问题。你的例子中的代码,
但我不太了解这方面的帮助。
>There may be something wrong with the "re" code in your example,
but I don''t know enough about that to help in that area.



其中有一个流浪的领先空间。

There is a stray leading space in it.


这篇关于堆栈实验的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆