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


Python heapq.heapreplace方法代码示例

本文整理汇总了Python中heapq.heapreplace方法的典型用法代码示例。如果您正苦于以下问题:Python heapq.heapreplace方法的具体用法?Python heapq.heapreplace怎么用?Python heapq.heapreplace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在heapq的用法示例。


在下文中一共展示了heapq.heapreplace方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: add

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [as 别名]
def add(self, item, priority=None):
        """
        Add element to the queue, with a separate priority if desired.

        Element will not be added if the queue is at capacity and the element
        has lower priority than the lowest currently in the queue.

        :param item: item to add to queue.
        :param priority: priority to use for item. if None (default), will use
                         the item itself to calculate its own priority.
        """
        if priority is None:
            priority = item
        if len(self.lst) < self.capacity:
            heapq.heappush(self.lst, (priority, item))
        elif priority > self.lst[0][0]:
            heapq.heapreplace(self.lst, (priority, item)) 
开发者ID:facebookresearch,项目名称:ParlAI,代码行数:19,代码来源:ir_baseline.py

示例2: mergeSort

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [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

示例3: add

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [as 别名]
def add(self, item, priority=None):
        """Add element to the queue, with a separate priority if desired.

        Element will not be added if the queue is at capacity and the element
        has lower priority than the lowest currently in the queue.

        :param item: item to add to queue.
        :param priority: priority to use for item. if None (default), will use
                         the item itself to calculate its own priority.
        """
        if priority is None:
            priority = item
        if len(self.lst) < self.capacity:
            heapq.heappush(self.lst, (priority, item))
        elif priority > self.lst[0][0]:
            heapq.heapreplace(self.lst, (priority, item)) 
开发者ID:natashamjaques,项目名称:neural_chat,代码行数:18,代码来源:ir_baseline.py

示例4: merge

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [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

示例5: kNN_entity

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [as 别名]
def kNN_entity(self, vec, topk=10, method=0, self_vec_id=None):
        q = []
        for i in range(len(self.vec_e)):
            #skip self
            if self_vec_id != None and i == self_vec_id:
                continue
            if method == 1:
                dist = SP.distance.cosine(vec, self.vec_e[i])
            else:
                dist = LA.norm(vec - self.vec_e[i])
            if len(q) < topk:
                HP.heappush(q, self.index_dist(i, dist))
            else:
                #indeed it fetches the biggest
                tmp = HP.nsmallest(1, q)[0]
                if tmp.dist > dist:
                    HP.heapreplace(q, self.index_dist(i, dist) )
        rst = []
        while len(q) > 0:
            item = HP.heappop(q)
            rst.insert(0, (self.vocab_e[self.vec2e[item.index]], item.dist))
        return rst

    #given entity name, find kNN 
开发者ID:muhaochen,项目名称:MTransE,代码行数:26,代码来源:TransE.py

示例6: kNN_relation

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [as 别名]
def kNN_relation(self, vec, topk=10, method=0, self_vec_id=None):
        q = []
        for i in range(len(self.vec_r)):
            #skip self
            if self_vec_id != None and i == self_vec_id:
                continue
            if method == 1:
                dist = SP.distance.cosine(vec, self.vec_r[i])
            else:
                dist = LA.norm(vec - self.vec_r[i])
            if len(q) < topk:
                HP.heappush(q, self.index_dist(i, dist))
            else:
                #indeed it fetches the biggest
                tmp = HP.nsmallest(1, q)[0]
                if tmp.dist > dist:
                    HP.heapreplace(q, self.index_dist(i, dist) )
        rst = []
        while len(q) > 0:
            item = HP.heappop(q)
            rst.insert(0, (self.vocab_r[self.vec2r[item.index]], item.dist))
        return rst

    #given relation name, find kNN 
开发者ID:muhaochen,项目名称:MTransE,代码行数:26,代码来源:TransE.py

示例7: test_nbest

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [as 别名]
def test_nbest(self):
        # Less-naive "N-best" algorithm, much faster (if len(data) is big
        # enough <wink>) than sorting all of data.  However, if we had a max
        # heap instead of a min heap, it could go faster still via
        # heapify'ing all of data (linear time), then doing 10 heappops
        # (10 log-time steps).
        data = [random.randrange(2000) for i in range(1000)]
        heap = data[:10]
        heapify(heap)
        for item in data[10:]:
            if item > heap[0]:  # this gets rarer the longer we run
                heapreplace(heap, item)
        self.assertEqual(list(heapiter(heap)), sorted(data)[-10:])

        self.assertRaises(TypeError, heapreplace, None)
        self.assertRaises(TypeError, heapreplace, None, None)
        self.assertRaises(IndexError, heapreplace, [], None) 
开发者ID:ofermend,项目名称:medicare-demo,代码行数:19,代码来源:test_heapq.py

示例8: _iter

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [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

示例9: mine_next

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [as 别名]
def mine_next(self):
        h = []
        for i in range(self.hp):
            v = random.randint(0, HASH_MAX)
            if len(h) < K:
                heapq.heappush(h, -v)
            elif v < -h[0]:
                heapq.heapreplace(h, -v)

        ret = [-i for i in h]
        ret.sort()
        return ret 
开发者ID:QuarkChain,项目名称:pyquarkchain,代码行数:14,代码来源:dual_pow_sim_with_order_stats.py

示例10: pick_weighted_random_elements

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [as 别名]
def pick_weighted_random_elements(iterable, k):
	queue = []
	for elem, weight in iterable:
		if not weight:
			continue
		r = random.random() ** (1 / weight)
		if len(queue) < k:
			heapq.heappush(queue, (r, elem))
		elif queue[0][0] < r:
			heapq.heapreplace(queue, (r, elem))
	return [elem for r, elem in queue] 
开发者ID:mrphlip,项目名称:lrrbot,代码行数:13,代码来源:utils.py

示例11: push_to_heap

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [as 别名]
def push_to_heap(self, stats, output_path):
        """Push stats and output path to the heap."""

        path_to_remove = None
        # The second element (`-self._validation_epoch`) serves as a timestamp
        # to ensure that in case of a tie, the earliest model is saved.
        heap_element = (stats, -self._validation_epoch, output_path)
        if self._validation_epoch <= self.keep_only_best:
            heapq.heappush(self.best_stats_summary, heap_element)
        else:
            worst_stat = heapq.heapreplace(
                self.best_stats_summary, heap_element
            )
            path_to_remove = str(worst_stat[2])  # Worst output path
        return path_to_remove 
开发者ID:Unbabel,项目名称:OpenKiwi,代码行数:17,代码来源:callbacks.py

示例12: add

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [as 别名]
def add(self, key, value):
        if self.isUnderfull:
            if len(self.primary) < self.inMemoryLimit:
                heappush(self.primary, (key, value))
                return
            else:
                self.isUnderfull = False
                assert self.currentFile is None
                self.currentFile = FileWriter(self.newFile())

        if key < self.primary[0][0]:
            heappush(self.secondary, (key, value))
            key, value = heappop(self.primary)
        else:
            key, value = heapreplace(self.primary, (key, value))

        while self.primary and self.primary[0][0] == key:
            value += heappop(self.primary)[1]
        self.currentFile.write((key, value))
        self.nStoredItems += 1

        if not self.primary:
            self.primary = self.secondary
            self.secondary = []
            self.currentFile.close()
            self.currentFile = None
            self.isUnderfull = True 
开发者ID:sequitur-g2p,项目名称:sequitur-g2p,代码行数:29,代码来源:mGramCounts.py

示例13: merge_iter

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [as 别名]
def merge_iter(iters, pfreq, pfunc, pfinal, key=None):
    if key:
        samekey = lambda e, pe: getattr(e, key) == getattr(pe, key, None)
    else:
        samekey = operator.eq
    count = 0
    total = sum(len(it) for it in iters)
    iters = (iter(it) for it in iters)
    heap = ((next(it),it) for it in iters)
    heap = [(e,it) for e,it in heap if e]

    heapq.heapify(heap)
    pe = None
    while heap:
        if not count % pfreq:
            pfunc(count, total)
        e, it = heap[0]
        if not samekey(e, pe):
            pe = e
            yield e
        count += 1
        try:
            e = it.next() # Don't use next() function, it's too expensive
        except StopIteration:
            heapq.heappop(heap) # remove current
        else:
            heapq.heapreplace(heap, (e, it)) # shift current to new location
    pfinal(count, total) 
开发者ID:omererdem,项目名称:honeything,代码行数:30,代码来源:helpers.py

示例14: pushpop

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [as 别名]
def pushpop(self, item):
        "Push the item onto the heap and then pop the smallest value"
        if self and self[0] < item:
            return heapq.heapreplace(self, item)
        return item 
开发者ID:ActiveState,项目名称:code,代码行数:7,代码来源:recipe-437116.py

示例15: kNN_entity

# 需要导入模块: import heapq [as 别名]
# 或者: from heapq import heapreplace [as 别名]
def kNN_entity(self, vec, tgt_lan='en', topk=10, method=0, self_vec_id=None, replace_q=True):
        q = []
        model = self.models.get(tgt_lan)
        if model == None:
            print "Model for language", tgt_lan," does not exist."
            return None
        for i in range(len(model.vec_e)):
            #skip self
            if self_vec_id != None and i == self_vec_id:
                continue
            if method == 1:
                dist = SP.distance.cosine(vec, model.vec_e[i])
            else:
                dist = LA.norm(vec - model.vec_e[i])
            if (not replace_q) or len(q) < topk:
                HP.heappush(q, model.index_dist(i, dist))
            else:
                #indeed it fetches the biggest
                tmp = HP.nsmallest(1, q)[0]
                if tmp.dist > dist:
                    HP.heapreplace(q, model.index_dist(i, dist) )
        rst = []
        if replace_q:
            while len(q) > 0:
                item = HP.heappop(q)
                rst.insert(0, (model.vocab_e[model.vec2e[item.index]], item.dist))
        else:
            while len(q) > topk:
                HP.heappop(q)
            while len(q) > 0:
                item = HP.heappop(q)
                rst.insert(0, (model.vocab_e[model.vec2e[item.index]], item.dist))
        return rst

    #given entity name, find kNN 
开发者ID:muhaochen,项目名称:MTransE,代码行数:37,代码来源:MMTransE.py


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