51 lines
1.1 KiB
ReStructuredText
51 lines
1.1 KiB
ReStructuredText
>>> import heapq
|
|
|
|
>>> heap = []
|
|
>>> heapq.heappush(heap, 1)
|
|
>>> heapq.heappush(heap, 3)
|
|
>>> heapq.heappush(heap, 5)
|
|
>>> heapq.heappush(heap, 2)
|
|
>>> heapq.heappush(heap, 4)
|
|
>>> heapq.nsmallest(3, heap)
|
|
[1, 2, 3]
|
|
|
|
------------------------------------------------------------------
|
|
|
|
>>> def push(*args, **kwargs):
|
|
... return heapq.heappush(heap, *args, **kwargs)
|
|
|
|
------------------------------------------------------------------
|
|
|
|
>>> import functools
|
|
>>> import heapq
|
|
|
|
>>> heap = []
|
|
>>> push = functools.partial(heapq.heappush, heap)
|
|
>>> smallest = functools.partial(heapq.nsmallest, iterable=heap)
|
|
|
|
>>> push(1)
|
|
>>> push(3)
|
|
>>> push(5)
|
|
>>> push(2)
|
|
>>> push(4)
|
|
>>> smallest(3)
|
|
[1, 2, 3]
|
|
|
|
------------------------------------------------------------------
|
|
|
|
>>> lambda_push = lambda x: heapq.heappush(heap, x)
|
|
|
|
>>> heapq.heappush
|
|
<built-in function heappush>
|
|
>>> push
|
|
functools.partial(<built-in function heappush>, [1, 2, 5, 3, 4])
|
|
>>> lambda_push
|
|
<function <lambda> at ...>
|
|
|
|
>>> heapq.heappush.__doc__
|
|
'Push item onto heap, maintaining the heap invariant.'
|
|
>>> push.__doc__
|
|
'partial(func, *args, **keywords) - new function ...'
|
|
>>> lambda_push.__doc__
|
|
|