我不知道如何保持这种简单...我希望有人也看看我的代码,并告诉我为什么我的一个函数不能像它应该那样工作......
我有一堂课:
class PriorityQueue(object):
'''A class that contains several methods dealing with queues.'''
def __init__(self):
'''The default constructor for the PriorityQueue class, an empty list.'''
self.q = []
def insert(self, number):
'''Inserts a number into the queue, and then sorts the queue to ensure that the number is in the proper position in the queue.'''
self.q.append(number)
self.q.sort()
def minimum(self):
'''Returns the minimum number currently in the queue.'''
return min(self.q)
def removeMin(self):
'''Removes and returns the minimum number from the queue.'''
return self.q.pop(0)
def __len__(self):
'''Returns the size of the queue.'''
return self.q.__len__()
def __str__(self):
'''Returns a string representing the queue.'''
return "{}".format(self.q)
def __getitem__(self, key):
'''Takes an index as a parameter and returns the value at the given index.'''
return self.q[key]
def __iter__(self):
return self.q.__iter__()我有这个函数需要一个文本文件,并通过我的类中的一些方法运行它:
def testQueue(fname):
infile = open(fname, 'r')
info = infile.read()
infile.close()
info = info.lower()
lstinfo = info.split()
queue = PriorityQueue()
for item in range(len(lstinfo)):
if lstinfo[item] == "i":
queue.insert(eval(lstinfo[item + 1]))
if lstinfo[item] == "s":
print(queue)
if lstinfo[item] == "m":
queue.minimum()
if lstinfo[item] == "r":
queue.removeMin()
if lstinfo[item] == "l":
len(queue)
#if lstinfo[item] == "g":不适合我的是我对queue.minimum和queue.removeMin()的电话。
我完全困惑,因为如果我在shell中手动执行它,这一切都有效,当我正在阅读文件并从我的文件中的字母中获取指令时,它也可以工作,但minimum和removeMin()不会显示但是,removeMin()将从列表中移除最小的数字。
我做错了什么,它没有显示它在做什么,就像类方法定义的那样?
IE:
def minimum(self):
return min(self.q)当我从我的功能调用它时不应该显示最小值?