Python优先级队列实现,属性错误
目前正在为我的CS课程做作业,我在实施PriorityQueue时遇到了一个问题。首先,这是我的init课程:Python优先级队列实现,属性错误,python,priority-queue,Python,Priority Queue,目前正在为我的CS课程做作业,我在实施PriorityQueue时遇到了一个问题。首先,这是我的init课程: class PriorityQueue(Container): def __init__(self, less_than): """Initialize this to an empty PriorityQueue. @type self: PriorityQueue @type less
class PriorityQueue(Container):
def __init__(self, less_than):
"""Initialize this to an empty PriorityQueue.
@type self: PriorityQueue
@type less_than: Callable[[Object, Object], bool]
Determines the relative priority of two elements of the queue.
If x._less_than(y) is true, then x has higher priority than y.
@rtype: None
"""
self._queue = []
self._less_than = less_than
但是,在为add方法运行doctests时,返回了一个属性错误,指出str对象没有小于的属性。我和我的教授谈过,他把它当作“可能只是一个打字错误”而不予理睬,所以经过几个小时的思考,我变成了这样
以下是添加方法:
def add(self, item):
if self._queue == []:
self._queue.append(item)
for i in range(len(self._queue)):
if item._less_than(self._queue[i]):
self._queue.insert(i, item)
下面是错误:
File ", line 99, in add
Failed example:
pq.add('arju')
Exception raised:
Traceback (most recent call last):
File "/Applications/PyCharm
CE.app/Contents/helpers/pycharm/docrunner.py", line 140, in __run
compileflags, 1), test.globs)
File "<doctest add[3]>", line 1, in <module>
pq.add('arju')
File "", line 113, in add
if item._less_than(self._queue[i]):
AttributeError: 'str' object has no attribute '_less_than'
文件”,第99行,添加
失败示例:
pq.添加(‘arju’)
提出的例外情况:
回溯(最近一次呼叫最后一次):
文件“/应用程序/PyCharm
CE.app/Contents/helpers/pycharm/docrunner.py”,第140行,正在运行
compileflags,1),test.globs)
文件“”,第1行,在
pq.添加(‘arju’)
文件“”,第113行,添加
如果项目小于(自我队列[i]):
AttributeError:“str”对象没有属性“\u小于”
任何帮助都将不胜感激
谢谢。错误信息非常清楚:
if item._less_than(self._queue[i]):
AttributeError: 'str' object has no attribute '_less_than'
这显然告诉我们,
项
上找不到\u less\u than
。而且项
的类型显然是str
。由于\u less\u than
是队列的类成员变量,而不是队列的元素,因此需要在队列上调用它,而不是在项目上。此外,根据do,\u less\u than
您发布的文档包含两个参数,而不是一个。错误消息非常清楚:
if item._less_than(self._queue[i]):
AttributeError: 'str' object has no attribute '_less_than'
这显然告诉我们,
项
上找不到\u less\u than
。而且项
的类型显然是str
。由于\u less\u than
是队列的类成员变量,而不是队列的元素,因此需要在队列上调用它,而不是在项目上。此外,根据do,\u less\u than
您发布的文档包含两个参数,而不是一个。您的add方法应该是这样的:
def add(self, item):
if not self._queue: # 1
self._queue.append(item)
return
for i in range(len(self._queue)):
if self._less_than(self._queue[i], item): # 2
self._queue.insert(i, item)
return
self._queue.append(item) # 3
有三件事需要改变
self.\u queue
与一个新的空队列进行比较。相反,您应该利用Python容器对象的布尔值为False
的事实,如果它们是空的。当您插入列表中的第一项时,您已经完成并需要返回;否则,您将在第二次插入相同的项我\u less\u than
是一个函数。它是在构造函数中分配的,因此它是优先级队列
的成员。它的目的是比较两个项目。因此需要使用两个要比较的项目调用它:其中一个是列表中的下一个项目,另一个是要插入的新项目。一旦找到正确的位置,to插入新项目,插入后即完成。此时必须返回\u less\u than
函数为列表中已存在的每个项目返回False
,则需要在末尾添加该项目您的add方法应该如下所示:
def add(self, item):
if not self._queue: # 1
self._queue.append(item)
return
for i in range(len(self._queue)):
if self._less_than(self._queue[i], item): # 2
self._queue.insert(i, item)
return
self._queue.append(item) # 3
有三件事需要改变
self.\u queue
与一个新的空队列进行比较。相反,您应该利用Python容器对象的布尔值为False
的事实,如果它们是空的。当您插入列表中的第一项时,您已经完成并需要返回;否则,您将在第二次插入相同的项我\u less\u than
是一个函数。它是在构造函数中分配的,因此它是优先级队列
的成员。它的目的是比较两个项目。因此需要使用两个要比较的项目调用它:其中一个是列表中的下一个项目,另一个是要插入的新项目。一旦找到正确的位置,to插入新项目,插入后即完成。此时必须返回\u less\u than
函数为列表中已存在的每个项目返回False
,则需要在末尾添加该项目您不应该在遍历
\u队列
列表时对其进行变异项
没有一个\u小于
。优先级队列的\u小于
。您也有其他错误。您正在将str
传递给添加
的项
参数,就在这里:pq.add('arju'))
。在add方法中,您使用项
,但是字符串没有这种方法…从错误中可以很清楚地看出…@Azat他不是在遍历列表,而是在遍历一系列整数,然后使用这些整数通过索引访问项目。这完全可以。在遍历\u queue
列表时,不应该对其进行变异项目
没有>_小于
。优先级队列有一个\u小于
。您还有其他错误。您正在将str
传递给add
的项
参数,就在这里:pq.add('arju')
。在您的add方法中,您使用项。然后(self.\u queue[i])
,但是字符串没有这种方法…从错误中可以很清楚地看出…@Azat他不是在列表中迭代,而是在一系列整数中迭代,然后使用这些整数通过索引访问项目。这完全可以。