Python heapq.heapreplace() Examples
The following are 30
code examples of heapq.heapreplace().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
heapq
, or try the search function
.
Example #1
Source File: TransE.py From MTransE with Apache License 2.0 | 6 votes |
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
Example #2
Source File: TransE.py From MTransE with Apache License 2.0 | 6 votes |
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
Example #3
Source File: ical.py From ccs-calendarserver with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: ir_baseline.py From neural_chat with MIT License | 6 votes |
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))
Example #5
Source File: test_heapq.py From medicare-demo with Apache License 2.0 | 6 votes |
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)
Example #6
Source File: IterMap.py From sequitur-g2p with GNU General Public License v2.0 | 6 votes |
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) # ---------------------------------------------------------------------------
Example #7
Source File: ir_baseline.py From ParlAI with MIT License | 6 votes |
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))
Example #8
Source File: MMTransE.py From MTransE with Apache License 2.0 | 5 votes |
def kNN_relation(self, vec, tgt_lan='fr', topk=10, method=0, self_vec_id=None): 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_r)): #skip self if self_vec_id != None and i == self_vec_id: continue if method == 1: dist = SP.distance.cosine(vec, model.vec_r[i]) else: dist = LA.norm(vec - model.vec_r[i]) if 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 = [] while len(q) > 0: item = HP.heappop(q) rst.insert(0, (model.vocab_r[model.vec2r[item.index]], item.dist)) return rst #given relation name, find kNN
Example #9
Source File: rrule.py From aws-extender with MIT License | 5 votes |
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
Example #10
Source File: load_utils.py From pheweb with GNU Affero General Public License v3.0 | 5 votes |
def add_and_keep_size(self, item, priority, size, popped_callback=None): if len(self._q) < size: self.add(item, priority) else: if -priority > self._q[0][0]: # if the new priority isn't as big as the biggest priority in the heap, switch them _, _, item = heapq.heapreplace(self._q, (-priority, MaxPriorityQueue.ComparesFalse(), item)) if popped_callback: popped_callback(item)
Example #11
Source File: rrule.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
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
Example #12
Source File: topk.py From python_data_structures_and_algorithms with MIT License | 5 votes |
def push(self, val): if len(self.minheap) >= self.capacity: min_val = self.minheap[0] if val < min_val: # 当然你可以直接 if val > min_val操作,这里我只是显示指出跳过这个元素 pass else: heapq.heapreplace(self.minheap, val) # 返回并且pop堆顶最小值,推入新的 val 值并调整堆 else: heapq.heappush(self.minheap, val) # 前面 k 个元素直接放入minheap
Example #13
Source File: test_heapq.py From medicare-demo with Apache License 2.0 | 5 votes |
def test_non_sequence(self): for f in (heapify, heappop): self.assertRaises(TypeError, f, 10) for f in (heappush, heapreplace, nlargest, nsmallest): self.assertRaises(TypeError, f, 10, 10)
Example #14
Source File: test_heapq.py From medicare-demo with Apache License 2.0 | 5 votes |
def test_len_only(self): for f in (heapify, heappop): self.assertRaises(TypeError, f, LenOnly()) for f in (heappush, heapreplace): self.assertRaises(TypeError, f, LenOnly(), 10) for f in (nlargest, nsmallest): self.assertRaises(TypeError, f, 2, LenOnly())
Example #15
Source File: test_heapq.py From medicare-demo with Apache License 2.0 | 5 votes |
def test_get_only(self): for f in (heapify, heappop): self.assertRaises(TypeError, f, GetOnly()) for f in (heappush, heapreplace): self.assertRaises(TypeError, f, GetOnly(), 10) for f in (nlargest, nsmallest): self.assertRaises(TypeError, f, 2, GetOnly())
Example #16
Source File: test_heapq.py From medicare-demo with Apache License 2.0 | 5 votes |
def test_get_only(self): seq = [CmpErr(), CmpErr(), CmpErr()] for f in (heapify, heappop): self.assertRaises(ZeroDivisionError, f, seq) for f in (heappush, heapreplace): self.assertRaises(ZeroDivisionError, f, seq, 10) for f in (nlargest, nsmallest): self.assertRaises(ZeroDivisionError, f, 2, seq)
Example #17
Source File: rrule.py From elasticintel with GNU General Public License v3.0 | 5 votes |
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
Example #18
Source File: rrule.py From bazarr with GNU General Public License v3.0 | 5 votes |
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
Example #19
Source File: rrule.py From CogAlg with MIT License | 5 votes |
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
Example #20
Source File: rrule.py From aws-builders-fair-projects with Apache License 2.0 | 5 votes |
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
Example #21
Source File: rrule.py From aws-builders-fair-projects with Apache License 2.0 | 5 votes |
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
Example #22
Source File: rrule.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
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
Example #23
Source File: rrule.py From android_universal with MIT License | 5 votes |
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
Example #24
Source File: heap_examples.py From Algorithm_Templates with MIT License | 5 votes |
def mergeKLists2(lists): # should use wrapper class to override __lt__ class Wrapper: def __init__(self, node): self.node = node def __lt__(self, other): return self.node.val < other.node.val head = point = ListNode(0) q = [] for l in lists: if l: heapq.heappush(q, Wrapper(l)) while q: node = q[0].node point.next = node point = point.next node = node.next if node: # fast version than pop & push heapq.heapreplace(q, Wrapper(node)) else: heapq.heappop(q) return head.next # [855] https://leetcode.com/problems/exam-room/ # Return a class ExamRoom(int N) that exposes two functions: ExamRoom.seat() returning an int representing what # seat the student sat in, and ExamRoom.leave(int p) representing that the student in seat number p now leaves the room.
Example #25
Source File: test_primes.py From oldvdf-competition with Apache License 2.0 | 5 votes |
def prime_iter(max_value=None): yield 2 heap = [(4, 2)] c = 2 while max_value is None or c < max_value: c += 1 n, p = heap[0] if n > c: yield c heapq.heappush(heap, (c+c, c)) while n <= c: heapq.heapreplace(heap, (n+p, p)) n, p = heap[0]
Example #26
Source File: test_mod.py From oldvdf-competition with Apache License 2.0 | 5 votes |
def primes(max_value=None): yield 2 heap = [(4, 2)] c = 2 while max_value is None or c < max_value: c += 1 n, p = heap[0] if n > c: yield c heapq.heappush(heap, (c+c, c)) while n <= c: heapq.heapreplace(heap, (n+p, p)) n, p = heap[0]
Example #27
Source File: rrule.py From deepWordBug with Apache License 2.0 | 5 votes |
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
Example #28
Source File: rrule.py From recruit with Apache License 2.0 | 5 votes |
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
Example #29
Source File: dual_pow_sim_with_order_stats.py From pyquarkchain with MIT License | 5 votes |
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
Example #30
Source File: utils.py From lrrbot with Apache License 2.0 | 5 votes |
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]