當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。