我在python中遇到了我的PriorityQueue类问题。我想我一直都是这样设置的,现在我正试着测试它,但是我一直在接受这个词#34;没有"当我打印出我正在排队的价值时,这是不对的。
这是我的测试代码,之后是我的输出和预期输出:
from PriorityQueue import PriorityQueue
PQ = PriorityQueue()
print(PQ.enqueue(1, 10))
print(PQ.enqueue(2, 5))
print(PQ.enqueue(3, 90))
print(PQ)
print(PQ.size())
输出:
None
None
None
<PriorityQueue.PriorityQueue object at 0x01EE5250>
2
预期产出:
10
5
90
90,5,10
2
为什么要打印单词None?我不知道为什么会这样做。这是我的PriorityQueue类,它从中获取函数。
#PriorityQueue.py
from MyHeap import Heap
class PriorityQueue(object):
def __init__(self):
self.heap = Heap()
def enqueue(self, priority, item):
'''Post: Item is inserted with specified priority in the PQ.'''
self.heap.insert((priority, item))
def first(self):
'''Post: Returns but does not remove the highest priority item from the PQ.'''
return self.heap.size()
def dequeue(self):
'''Post: Removes and returns the highest priority item from the PQ.'''
if self.heap.size() is None:
raise ValueError("Error your queue is empty.")
x = self.first()
self.heap.delete_max()
return x
def size(self):
'''Post: Returns the number of items in the PQ.'''
return self.heap.size()
所有这些函数,或者大部分函数都取自我的自定义堆类。
class Heap(object):
def __init__(self, items=None):
'''Post: A heap is created with specified items.'''
self.heap = [None]
if items is None:
self.heap_size = 0
else:
self.heap += items
self.heap_size = len(items)
self._build_heap()
def size(self):
'''Post: Returns the number of items in the heap.'''
return self.heap_size
def _heapify(self, position):
'''Pre: Items from 0 to position - 1 satisfy the Heap property.
Post: Heap Property is satisfied for the entire heap.'''
item = self.heap[position]
while position * 2 <= self.heap_size:
child = position * 2
# If the right child, determine the maximum of two children.
if (child != self.heap_size and self.heap[child+1] > self.heap[child]):
child += 1
if self.heap[child] > item:
self.heap[position] = self.heap[child]
position = child
else:
break
self.heap[position] = item
def delete_max(self):
'''Pre: Heap property is satisfied
Post: Maximum element in heap is removed and returned. '''
if self.heap_size > 0:
max_item = self.heap[1]
self.heap[1] = self.heap[self.heap_size]
self.heap_size -= 1
self.heap.pop()
if self.heap_size > 0:
self._heapify(1)
return max_item
def insert(self, item):
'''Pre: Heap Property is Satisfied.
Post: Item is inserted in proper location in heap.'''
self.heap_size += 1
# extend the length of the list.
self.heap.append(None)
position = self.heap_size
parent = position // 2
while parent > 0 and self.heap[parent] < item:
# Move the item down.
self.heap[position] = self.heap[parent]
position = parent
parent = position // 2
# Puts the new item in the correct spot.
self.heap[position] = item
def _build_heap(self):
''' Pre: Self.heap has values in 1 to self.heap_size
Post: Heap property is satisfied for entire heap. '''
# 1 through self.heap_size.
for i in range(self.heap_size // 2, 0, -1): # Stops at 1.
self._heapify(i)
def heapsort(self):
'''Pre: Heap Property is satisfied.
Post: Items are sorted in self.heap[1:self.sorted_size].'''
sorted_size = self.heap_size
for i in range(0, sorted_size -1):
# Since delete_max calls pop to remove an item, we need to append a dummy value to avoid an illegal index.
self.heap.append(None)
item = self.delete_max()
self.heap[sorted_size - i] = item
有人能告诉我为什么它不起作用的问题吗?我以为应该归还价值。我也知道我的PriorityQueue课程需要 iter ,但我不知道它会是什么样子。有谁可以帮助我?
答案 0 :(得分:1)
您的enqueue
方法没有return
语句,因此返回None
。
如果您期望Python类似于Ruby或Scheme,并返回评估的最后一个表达式的值 - 那么,Python不是Ruby;其中一个最大的区别是,与Ruby不同,Python是一种基于语句的语言,并不是所有东西都是表达式,所以通常 no&#34; last expression&#34 ;,只是最后一个声明,没有价值。
同时,从变异None
的方法返回self
被认为是Pythonic要做的事情(例如,参见list.append
),所以你实际上正确编写了你的代码,即使你并不打算。 :)
这意味着Python并不适合方法链接和流畅的风格&#34;在其他一些语言中很受欢迎。这是一个深思熟虑的设计选择;虽然Guido从来没有真正阐明过为什么,但是考虑到单个巨型表达式并不能让你看到通过垂直空间和水平缩进的控制流程,就像一系列语句所做的那样,不鼓励你给有意义的中间值赋予有意义的名称,使用回溯或传统的步调试器等进行调试更难。