在python中有没有一种附加到对象的方法?优先级队列
目前,我的排队函数出现了一个错误。当我尝试向对象列表中追加一个数字时,它会显示“'set'对象没有'append'属性”。我假设问题与我如何传递列表有关,但这是我目前的问题。我有一个10号的硬编码列表要处理,因为在我知道发生了什么之前,我不想做一个更大的列表。任何帮助都将不胜感激。另外,我在代码中的注释也是我希望作为最终结果所做的。如果你对此有任何意见,那将非常有用。不过现在,我想知道如何避免这个错误。多谢各位在python中有没有一种附加到对象的方法?优先级队列,python,list,priority-queue,implementation,Python,List,Priority Queue,Implementation,目前,我的排队函数出现了一个错误。当我尝试向对象列表中追加一个数字时,它会显示“'set'对象没有'append'属性”。我假设问题与我如何传递列表有关,但这是我目前的问题。我有一个10号的硬编码列表要处理,因为在我知道发生了什么之前,我不想做一个更大的列表。任何帮助都将不胜感激。另外,我在代码中的注释也是我希望作为最终结果所做的。如果你对此有任何意见,那将非常有用。不过现在,我想知道如何避免这个错误。多谢各位 class PQ_List(object): def __init__(s
class PQ_List(object):
def __init__(self, sampleList):
print ("creates an unsorted list from passed in list")
self.list = sampleList
print (self.list)
#
# Returns the list
def enQueue(self, item):
print ("adds an item to the PQ")
self.list.append(item)
print (self.list)
# Add an item to the PQ
def deQueue(self):
print ("removes the highest priority item from the PQ")
self.list = self.list[1:]
print (self.list)
# Remove the highest priority item from the PQ
def sneakAPeek(self):
print ("returns the highest priority in the PQ, but does not remove it")
return self.list[0]
#
# Return the highest priority item from the PQ, but don't remove it
def isEmpty(self):
print ("returns T if PQ is empty, F if PQ has entries")
if len(self.list) > 0:
return 'F'
else:
return 'T'
# Return a T if PQ is empty, F if PQ is not empty
#
def size(self):
print ("returns number of items in queue")
return len(self.list)
# Return the number of items in the queue
sampleList = {1, 2, 5, 8, 4, 15, 13, 12, 10, 6}
my_listPQ = PQ_List(sampleList) #print first 10 numbers, use size to prove the rest is there
my_listPQ.enQueue(1500)
my_listPQ.deQueue()
my_listPQ.sneakAPeek()
my_listPQ.isEmpty()
my_listPQ.size()
我希望输出会向enQueue函数的列表中添加1500个。然后执行以下功能。
任何帮助都将不胜感激 在python中,列表使用方括号
[
,]
,集合使用花括号{
,}
因此,改变路线
sampleList = {1, 2, 5, 8, 4, 15, 13, 12, 10, 6}
到
你可以走了。改变
sampleList = {1, 2, 5, 8, 4, 15, 13, 12, 10, 6} # this is set and don't have append
对于这一点:
sampleList = [1, 2, 5, 8, 4, 15, 13, 12, 10, 6] # this is list
发生了,不用担心。您的样本列表是一个集合,而不是一个列表,集合没有
append
方法。尝试将sampleList={…
替换为sampleList=[1,2,5,8,4,15,13,12,10,6]
sampleList = [1, 2, 5, 8, 4, 15, 13, 12, 10, 6] # this is list