首页 > 解决方案 > python TypeError:'list'和'int'的实例之间不支持'> ='

问题描述

我不断得到:TypeError: '>=' not supported between instances of 'list' and 'int'在第 40 行代码如下,当我打印 while 语句中的值时,它们以整数形式返回:

pQueue = [['A', 4], ['B', 15], ['A', 15], ['A', 12]]

class priorityQueue():
    def __init__(self,queue):
        self.queue = queue

    def sort(self):
        length = len(self.queue)
        tempListA = []
        tempListB = []
        tempListC = []

        # splice the list depending if the letter is a b or c 
        for i in range(0, length):
            tempItem = self.queue[i]
            if tempItem[0] == 'A':
                tempListA.append(tempItem)
                
            elif tempItem[0] == 'B':
                tempListB.append(tempItem)
                
            elif tempItem[0] == 'C':
                tempListC.append(tempItem)
                
        #sort each list via number in positon
        for i in range(0, len(tempListA)):
            valueA = tempListA[i]
            
            currentValueA = valueA[1]
            
            positonA = i
            
            a=tempListA[positonA-1]
            
            b=a[1]
            

            while (positonA>0) and (b>currentValueA):
                tempListA[positonA]=tempListA[positonA-1]
                positonA = [positonA-1]

            tempListA[positonA]=valueA

            print(tempListA)


        #sort each list via number in positon
        for i in range(0, len(tempListB)):
            valueB = tempListB[i]
            
            currentValueB = valueB[1]
            
            positonB = i
            
            a=tempListB[positonB-1]
            
            b=a[1]
            

            while (positonB>0) and (b>currentValueB):
                tempListB[positonB]=tempListB[positonB-1]
                positonB = [positonB-1]

            tempListB[positonB]=valueB

            print(tempListB)
        
        #sort each list via number in positon for c
        for i in range(0, len(tempListC)):
            valueC = tempListC[i]
            
            currentValueC = valueC[1]
            
            positonC = i
            
            a=tempListC[positonC-1]
            
            b=a[1]
            

            while (positonC>0) and (b>currentValueC):
                tempListC[positonC]=tempListC[positonC-1]
                positonC = [positonC-1]

            tempListC[positonC]=valueC

            print(tempListC)

        # merge the lists back together
        mergedQ = []
        for i in range(0, len(tempListA)):            
            a = tempListA[i]
            mergedQ.append(a)
            print(mergedQ)

        for i in range(0, len(tempListB)):            
            a = tempListB[i]
            mergedQ.append(a)
            print(mergedQ)

        for i in range(0, len(tempListC)):            
            a = tempListC[i]
            mergedQ.append(a)
            print(mergedQ)
                       
        
a = priorityQueue(pQueue)
a.sort()

上面代码的实际输出是:

[['A', 4], ['A', 15], ['A', 12]]
[['A', 4], ['A', 15], ['A', 12]]
Traceback (most recent call last):
  File "66737040.py", line 108, in <module>
    a.sort()
  File "66737040.py", line 38, in sort
    while (positonA>0) and (b>currentValueA):
TypeError: '>' not supported between instances of 'list' and 'int'

标签: pythonqueuetypeerrorpriority-queue

解决方案


推荐阅读