我是python和实现堆栈的新手。 我正在使用字典调用堆栈函数。 但是,push()要求我传递一个参数。 我该怎么办?
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 getStack(self):
return print(self.items)
s = stack()
switcher = {
'1' : s.push,
'2' : s.pop,
'3' : s.isEmpty,
'4' : s.getStack,
}
def dictionaryCall(key):
switcher[key]()
while(1):
key = input('enter choice 1.push 2. pop 3.isEmpty 4 getStack 5.exit: ')
if key == '5':
break
dictionaryCall(key)
答案 0 :(得分:1)
这是一个可行的实现。如您所见,您将必须向element
添加一个可选的第二个参数(此处称为dictionaryCall
),然后将其传递给选定的方法。
class stack():
def __init__(self):
self.items = []
def push(self,item):
self.items.append(item)
def pop(self):
print(self.items.pop())
def isEmpty(self):
# I dropped the `return` here due to syntax error
print (self.items == [])
def getStack(self):
# I dropped the `return` here due to syntax error
print (self.items)
s = stack()
switcher = {
'1' : s.push,
'2' : s.pop,
'3' : s.isEmpty,
'4' : s.getStack,
}
# I added an optional parameter for the `push` method
def dictionaryCall(key, element = None):
method = switcher[key]
# call the method with element if it exists
if element == None: method()
else: method(element)
# call from command line
if __name__ == '__main__':
while(1):
key = input('enter choice 1.push 2. pop 3.isEmpty 4 getStack 5.exit: ')
if key == '5':
break
print ('> {0}'.format(key))
if key == '1':
element = input('enter an element to push onto the stack: ')
dictionaryCall(key, element)
else: dictionaryCall(key)
示例:
enter choice 1.push 2. pop 3.isEmpty 4 getStack 5.exit: '1'
> 1
enter an element to push onto the stack: 'foo'
enter choice 1.push 2. pop 3.isEmpty 4 getStack 5.exit: '3'
> 3
False
enter choice 1.push 2. pop 3.isEmpty 4 getStack 5.exit: '4'
> 4
['foo']
enter choice 1.push 2. pop 3.isEmpty 4 getStack 5.exit:
您需要注意的几件事:
return print (...)
无效的语法(只写print (...)
)input
从命令行读取的'1'
才能推送元素,因为您的字典具有字符串键if __name__ == '__main__': ...
中,否则文件将被解析但不会执行pop
应该打印弹出的元素答案 1 :(得分:0)
在此版本中,push
参数与键位于同一行。这使代码更加复杂,但是使用起来更好一些。我们使用.strip
方法从输入字符串中删除任何前导或尾随空格。然后,在检查“ 5”退出键之后,如果输入行长于1个字符,则将其拆分。该行的第一项成为键,空格后的后续字符成为push
函数的参数。
此代码还会在尝试使用密钥之前检查密钥是否有效。
我添加了import readline
,它可以对输入行进行行编辑和历史记录缓冲,因此您可以按向上箭头键转到上一行。至少,它可以在GNU系统上执行此操作,而在Windows上则不执行任何操作,尽管您可以使用第三方pyreadline来代替。
import readline
class Stack():
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
def pop(self):
# Return None if the stack is empty
if self.items:
return self.items.pop()
def isEmpty(self):
return not self.items
def getStack(self):
return self.items
stack = Stack()
switcher = {
'1' : stack.push,
'2' : stack.pop,
'3' : stack.isEmpty,
'4' : stack.getStack,
}
def dictionaryCall(key, val=None):
func = switcher[key]
if val is None:
return func()
else:
return func(val)
while True:
key = input('Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > ').strip()
if key == '5':
break
if len(key) > 1:
# There's a value after the key.
key, val = key.split(maxsplit=1)
else:
val = None
if key not in switcher:
print('Invalid choice')
continue
if key == '1' and val is None:
print('You must supply an item to push')
continue
result = dictionaryCall(key, val)
if result is not None:
print(result)
这是一个简短的演示
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 3
True
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 4
[]
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 1
You must supply an item to push
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 1 Hello
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 1 This is a test
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 4
['Hello', 'This is a test']
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 3
False
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 2
This is a test
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 2
Hello
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 2
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 3
True
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 4
[]
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 1 bye
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 4
['bye']
Enter choice: 1.push item 2.pop 3.isEmpty 4.getStack 5.exit > 5