本文整理汇总了Python中heapq.heapify方法的典型用法代码示例。如果您正苦于以下问题:Python heapq.heapify方法的具体用法?Python heapq.heapify怎么用?Python heapq.heapify使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类heapq
的用法示例。
在下文中一共展示了heapq.heapify方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: unload
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def unload(self, execute=True):
commands = CommandList()
undoWork = ConfigObject.prioritizeConfigs(self.currentConfig.values())
heapq.heapify(undoWork)
while undoWork:
prio, config = heapq.heappop(undoWork)
commands.extend(config.revert(self.currentConfig))
# Finally, execute the commands.
if execute and self.execCommands:
self.execute(commands)
self.previousCommands = commands
self.currentConfig = dict()
return True
示例2: merge
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def merge(lists):
merged_list = []
heap = [(lst[0], i, 0) for i, lst in enumerate(lists) if lst]
heapq.heapify(heap)
while heap:
#print(heap)
val, list_ind, element_ind = heapq.heappop(heap)
merged_list.append(val)
if element_ind + 1 < len(lists[list_ind]):
next_tuple = (lists[list_ind][element_ind + 1],
list_ind,
element_ind + 1)
heapq.heappush(heap, next_tuple)
return merged_list
#Test Cases
示例3: highFive
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def highFive(self, items):
"""
:type items: List[List[int]]
:rtype: List[List[int]]
"""
record = dict()
for item in items:
sd, sc = item[0], item[1]
if sd not in record:
record[sd] = [sc]
heapq.heapify(record[sd])
else:
heapq.heappush(record[sd], sc)
if len(record[sd]) > 5:
heapq.heappop(record[sd])
print record[sd]
res = []
for key, val in record.items():
res.append([key, sum(val) // 5])
# print record
return res
示例4: DeleteDigits_error
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def DeleteDigits_error(self, A, k):
"""
Remove and keep the n-k largest numbers
heap: O(n lg (n-k))
error in case: 254193, 1
:param A: A positive integer which has N digits, A is a string.
:param k: Remove k digits.
:return: A string
"""
lst = map(int, list(str(A)))
m = len(lst)-k
tuples = [(-lst[i], i) for i in xrange(m)] # negative sign for max heap
heapq.heapify(tuples)
for i in xrange(m, len(lst)):
if -tuples[0][0] > lst[i]:
heapq.heappop(tuples)
heapq.heappush(tuples, (-lst[i], i))
rets = [elt[1] for elt in tuples]
rets.sort()
rets = map(lambda x: str(lst[x]), rets)
return "".join(rets)
示例5: cancel
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def cancel(handle):
"""Provided its handle, cancels the execution of a timeout."""
handles = TimeoutScheduler._handles
with TimeoutScheduler._mutex:
if handle in handles:
# Time complexity is O(n)
handle._cb = None
handles.remove(handle)
heapq.heapify(handles)
if len(handles) == 0:
# set the event to stop the wait - this kills the thread
TimeoutScheduler._event.set()
else:
raise Scapy_Exception("Handle not found")
示例6: mergeSort
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def mergeSort(seqs):
"""
perform merge sort on a list of sorted iterators
"""
queue = []
for s in seqs:
s = assertIsSorted(s)
it = iter(s)
try:
queue.append((it.next(), it.next))
except StopIteration:
pass
heapq.heapify(queue)
while queue:
item, it = queue[0]
yield item
try:
heapq.heapreplace(queue, (it(), it))
except StopIteration:
heapq.heappop(queue)
# ---------------------------------------------------------------------------
示例7: schedule
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def schedule(plugin):
# List of scheduled calls with next runtime, function and interval
next_runs = [
(time() + 300, clear_temporary_exclusion, 300),
(time() + plugin.probe_interval, start_probe, plugin.probe_interval),
(time() + 1, poll_payments, 1),
]
heapq.heapify(next_runs)
while True:
n = heapq.heappop(next_runs)
t = n[0] - time()
if t > 0:
sleep(t)
# Call the function
n[1](plugin)
# Schedule the next run
heapq.heappush(next_runs, (time() + n[2], n[1], n[2]))
示例8: merge
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def merge(*iterables):
"""
Merge sorted iterables into one sorted iterable.
@param iterables: arguments are iterables which yield items in sorted order.
@return: an iterable of all items generated by every iterable in
C{iterables} in sorted order.
"""
heap = []
for iterable in iterables:
iterator = iter(iterable)
for value in iterator:
heap.append((value, iterator))
break
heapq.heapify(heap)
while heap:
value, iterator = heap[0]
yield value
for value in iterator:
heapq.heapreplace(heap, (value, iterator))
break
else:
heapq.heappop(heap)
示例9: mergeKLists
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def mergeKLists(self, lists: List[ListNode]) -> ListNode:
hp = []
nodes = list(lists)
for idx in range(len(nodes)):
node = nodes[idx]
if node is not None:
hp.append((node.val, idx))
heapq.heapify(hp)
head = None
prev = None
while hp:
val, idx = heapq.heappop(hp)
node = nodes[idx]
if head is None:
head = node
if prev is not None:
prev.next = node
ne = node.next
if ne is not None:
heapq.heappush(hp, (ne.val, idx))
nodes[idx] = ne
prev = node
return head
示例10: push_all
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def push_all(self, record_shard_pairs):
"""Push multiple (record, shard) pairs at once, with only one :meth:`heapq.heapify` call to maintain order.
:param record_shard_pairs: list of ``(record, shard)`` tuples
(see :func:`~bloop.stream.buffer.RecordBuffer.push`).
"""
# Faster than inserting one at a time; the heap is sorted once after all inserts.
for record, shard in record_shard_pairs:
item = heap_item(self.clock, record, shard)
self.heap.append(item)
heapq.heapify(self.heap)
示例11: _resort
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def _resort(self):
heapq.heapify(self.queue)
示例12: update_priority
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def update_priority(self) -> None:
heapq.heapify(self.vertices)
示例13: __next__
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def __next__(self):
try:
self.dt = advance_iterator(self.gen)
except StopIteration:
if self.genlist[0] is self:
heapq.heappop(self.genlist)
else:
self.genlist.remove(self)
heapq.heapify(self.genlist)
示例14: _iter
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def _iter(self):
rlist = []
self._rdate.sort()
self._genitem(rlist, iter(self._rdate))
for gen in [iter(x) for x in self._rrule]:
self._genitem(rlist, gen)
exlist = []
self._exdate.sort()
self._genitem(exlist, iter(self._exdate))
for gen in [iter(x) for x in self._exrule]:
self._genitem(exlist, gen)
lastdt = None
total = 0
heapq.heapify(rlist)
heapq.heapify(exlist)
while rlist:
ritem = rlist[0]
if not lastdt or lastdt != ritem.dt:
while exlist and exlist[0] < ritem:
exitem = exlist[0]
advance_iterator(exitem)
if exlist and exlist[0] is exitem:
heapq.heapreplace(exlist, exitem)
if not exlist or ritem != exlist[0]:
total += 1
yield ritem.dt
lastdt = ritem.dt
advance_iterator(ritem)
if rlist and rlist[0] is ritem:
heapq.heapreplace(rlist, ritem)
self._len = total
示例15: extend
# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def extend(self, items):
"""
Extend heap by an iterable object. The heap will be reheapified.
:param items: priority items
"""
self._req_heap.extend(items)
heapq.heapify(self._req_heap)