객체가 힙 내부에 있는지 확인하려고합니다.힙 내부의 객체인지 확인
이import heapq
class Heap(object):
def __init__(self):
self.heap = []
def push(self, priority, cell):
heapq.heappush(self.heap, (priority, cell))
def pop(self):
cell = heapq.heappop(self.heap)[1]
return cell
def contains(self, cell):
if(cell in self.heap):
return True
return False
def isEmpty(self):
return len(self.heap) == 0
이 셀 클래스 :
class Cell(object):
def __init__(self, x, y):
self.X = x
self.Y = y
def __eq__(self, other):
return int(self.X) == int(other.X) and int(self.Y) == int(other.Y)
내가 Heap
클래스를 사용 나는이 같은 봐 Heap
과 Cell
클래스가
AttributeError: 'tuple' object has no attribute 'X'
: 그러나, 나는이 오류가 계속 이렇게 : contains
메서드를 사용할 때 오류가 발생합니다.
from Heap import Heap
from Cell import Cell
class Test(object):
def __init__(self):
self.myHeap = Heap()
cell = Cell(2, 3)
self.myHeap.push(1, cell)
if self.myHeap.contains(cell) == False:
print("not in heap")
test = Test()
내가 뭘 잘못하고 있니? 어떤 도움을 주시면 감사하겠습니다.
'self.myHeap'은 위에서 만든'Heap' 클래스의'Heap' 객체입니다. 더 명확하게하기 위해 더 많은 코드가 추가되었습니다. –
'List'에서'priority'와'Object Cell'을 푸시하고 있습니까 ?? 그냥'셀'을리스트에 넣고'self.myHeap'은 무엇입니까? '자기'를 제거한 다음 당신이 무엇을 가지고 있는지 말해보십시오. –
자기가 정의한 것을 확인했습니다. 코드를 편집하지 못했습니다. –