from heapq A value is added or deleted to a set. I want to get the smallest value. ā†’ Prepare a heapq that contains the deleted values and skip reading it when acquiring. python

from heapq import *

added = []
removed = []
# add
heappush(added, 1)
heappush(added, 2)
# remove
heappush(removed, 1)
# get top
while removed and added[0] == removed[0]:
    heappop(added)
    heappop(removed)

print(added[0])  # => 2

This page is auto-translated from /nishio/ć‚ć‚‹é›†åˆć«å€¤ćŒčæ½åŠ å‰Šé™¤ć•ć‚Œć‚‹ć€‚ęœ€å°ć®å€¤ć‚’å–å¾—ć—ćŸć„ć€‚ using DeepL. If you looks something interesting but the auto-translated English is not good enough to understand it, feel free to let me know at @nishio_en. Iā€™m very happy to spread my thought to non-Japanese readers.