CKTIS / Python
heapq.heappop
Odstrani in vrne najmanjši element veljavne minimalne kopice.
Različica: Python 3.14; repository image, production runtime not attested
Sintaksa
heapq.heappop(heap, /)Odstrani in vrne najmanjši element veljavne minimalne kopice.
Po odstranitvi uredi kopico tako, da je v korenu spet najmanjši element glede na <. To velja tudi za nize. Vsak klic odstrani eno pojavitev tudi pri enakih prioritetah. Samo branje h[0] ohrani dolžino.
Po odstranitvi 1 in 4 tretji klic sproži IndexError.
from heapq import heappop
h = [2, 2, 5]
assert h[0] == 2 and len(h) == 3
assert heappop(h) == 2 and len(h) == 2 and h[0] == 2
assert heappop(h) == 2 and heappop(h) == 5
h = ["ant", "dog", "cat"]
assert heappop(h) == "ant" and h[0] == "cat"
Primeri
import heapq
heap = [1, 4]
print(heapq.heappop(heap), heapq.heappop(heap))
try:
heapq.heappop(heap)
except IndexError as error:
print(type(error).__name__)
Rezultat
1 4 IndexError
Po odstranitvi 1 in 4 tretji klic sproži IndexError.