当前位置: 首页>>代码示例>>Python>>正文


Python heapq.heapify方法代码示例

本文整理汇总了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 
开发者ID:ParadropLabs,项目名称:Paradrop,代码行数:19,代码来源:manager.py

示例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 
开发者ID:amitrajitbose,项目名称:Competitive_Programming,代码行数:23,代码来源:merge_k_Sorted_lists.py

示例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 
开发者ID:JiayangWu,项目名称:LeetCode-Python,代码行数:25,代码来源:1086-前五科的均分.py

示例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) 
开发者ID:algorhythms,项目名称:LintCode,代码行数:27,代码来源:Delete Digits.py

示例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") 
开发者ID:secdev,项目名称:scapy,代码行数:18,代码来源:isotp.py

示例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)

# --------------------------------------------------------------------------- 
开发者ID:sequitur-g2p,项目名称:sequitur-g2p,代码行数:24,代码来源:IterMap.py

示例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])) 
开发者ID:lightningd,项目名称:plugins,代码行数:21,代码来源:probe.py

示例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) 
开发者ID:apple,项目名称:ccs-calendarserver,代码行数:24,代码来源:ical.py

示例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 
开发者ID:franklingu,项目名称:leetcode-solutions,代码行数:25,代码来源:Solution.py

示例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) 
开发者ID:numberoverzero,项目名称:bloop,代码行数:13,代码来源:buffer.py

示例11: _resort

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def _resort(self):
        heapq.heapify(self.queue) 
开发者ID:binux,项目名称:pyspider,代码行数:4,代码来源:task_queue.py

示例12: update_priority

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapify [as 别名]
def update_priority(self) -> None:
        heapq.heapify(self.vertices) 
开发者ID:wangzheng0822,项目名称:algo,代码行数:4,代码来源:dijkstra.py

示例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) 
开发者ID:MediaBrowser,项目名称:plugin.video.emby,代码行数:11,代码来源:rrule.py

示例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 
开发者ID:MediaBrowser,项目名称:plugin.video.emby,代码行数:33,代码来源:rrule.py

示例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) 
开发者ID:mars-project,项目名称:mars,代码行数:9,代码来源:assigner.py


注:本文中的heapq.heapify方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。