在python中有没有附加到对象的方法?优先级队列

2024-05-29 07:32:39 发布

您现在位置:Python中文网/ 问答频道 /正文

目前,我得到一个错误,当它涉及到我的排队函数。当我试图将一个数字附加到我的对象列表时,它会说“'set'对象没有属性'append'”。我假设问题与我如何传递列表有关,但这是我目前的问题。我有一个10号的硬编码列表要处理,因为在我知道发生了什么之前,我不想做一个更大的列表。任何帮助都将不胜感激。我在代码中的注释也是我想要作为最终结果做的。如果你对此有任何意见,那将非常有用。不过,目前我只想弄清楚如何避免这个错误。非常感谢。你知道吗

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()

我希望输出会将1500添加到enQueue函数的列表中。然后执行以下功能。 任何帮助都将不胜感激!你知道吗


Tags: theinfromself列表ifmydef
2条回答

改变

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

在python中,使用方括号[]表示列表,大括号{}表示集合。你知道吗

因此,改变路线

sampleList = {1, 2, 5, 8, 4, 15, 13, 12, 10, 6}

sampleList = [1, 2, 5, 8, 4, 15, 13, 12, 10, 6]

你可以走了。你知道吗

相关问题 更多 >

    热门问题