本文整理匯總了Python中heapq.heappushpop方法的典型用法代碼示例。如果您正苦於以下問題:Python heapq.heappushpop方法的具體用法?Python heapq.heappushpop怎麽用?Python heapq.heappushpop使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類heapq
的用法示例。
在下文中一共展示了heapq.heappushpop方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _addResult
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def _addResult(self, test, *args):
try:
name = test.id()
except AttributeError:
name = 'Unknown.unknown'
test_class, test_name = name.rsplit('.', 1)
elapsed = (self._now() - self.start_time).total_seconds()
item = (elapsed, test_class, test_name)
if len(self.slow_tests) >= self.num_slow_tests:
heapq.heappushpop(self.slow_tests, item)
else:
heapq.heappush(self.slow_tests, item)
self.results.setdefault(test_class, [])
self.results[test_class].append((test_name, elapsed) + args)
self.last_time[test_class] = self._now()
self.writeTests()
示例2: search_docs
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def search_docs(inputs, max_ex=5, opts=None):
"""Given a set of document ids (returned by ranking for a question), search
for top N best matching (by heuristic) paragraphs that contain the answer.
"""
if not opts:
raise RuntimeError('Options dict must be supplied.')
doc_ids, q_tokens, answer = inputs
examples = []
for i, doc_id in enumerate(doc_ids):
for j, paragraph in enumerate(re.split(r'\n+', fetch_text(doc_id))):
found = find_answer(paragraph, q_tokens, answer, opts)
if found:
# Reverse ranking, giving priority to early docs + paragraphs
score = (found[0], -i, -j, random.random())
if len(examples) < max_ex:
heapq.heappush(examples, (score, found[1]))
else:
heapq.heappushpop(examples, (score, found[1]))
return [e[1] for e in examples]
示例3: push
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def push(self, score, item, extra_data=None):
"""Push an item onto the queue.
If the queue is at capacity, the item with the smallest score will be
dropped. Note that it is assumed each item has exactly one score. The same
item with a different score will still be dropped.
Args:
score: Number used to prioritize items in the queue. Largest scores are
kept in the queue.
item: A hashable item to be stored. Duplicates of this item will not be
added to the queue.
extra_data: An extra (possible not hashable) data to store with the item.
"""
if item in self.unique_items:
return
if len(self.heap) >= self.capacity:
_, popped_item, _ = heapq.heappushpop(
self.heap, MPQItemContainer(score, item, extra_data))
self.unique_items.add(item)
self.unique_items.remove(popped_item)
else:
heapq.heappush(self.heap, MPQItemContainer(score, item, extra_data))
self.unique_items.add(item)
示例4: scored_dict_ranking
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def scored_dict_ranking(candidate_doc_list, scored_dict, top_k):
scored_doc = []
v_terms = scored_dict.keys()
for cur_doc in candidate_doc_list:
cur_doc_score = 0
for cur_term in v_terms:
if cur_doc not in scored_dict[cur_term]:
cur_doc_score += 0
else:
cur_doc_score += scored_dict[cur_term][cur_doc]
if top_k is not None and 0 <= top_k == len(scored_doc):
heapq.heappushpop(scored_doc, (cur_doc_score, cur_doc))
else:
heapq.heappush(scored_doc, (cur_doc_score, cur_doc))
return scored_doc
示例5: nearest
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def nearest(self, x, near_k=1, p=2):
# use the max heap builtin library heapq
# init the elements with -inf, and use the minus distance for comparison
# the top of the max heap is the min distance.
self.knn = [(-np.inf, None)]*near_k
def visit(node):
if not node == None:
# cal the distance to the split point, i.e. the hyperplane
dis = x[node.sp] - node.data[node.sp]
# visit the child node recursively
# if returned, we get the current nearest point
visit(node.left if dis < 0 else node.right)
# cal the distance to the current nearest point
curr_dis = np.linalg.norm(x-node.data, p)
# push the minus distance to the heap
heapq.heappushpop(self.knn, (-curr_dis, node))
# compare the distance to the hyperplane with the min distance
# if less, visit another node.
if -(self.knn[0][0]) > abs(dis):
visit(node.right if dis < 0 else node.left)
visit(self.root)
self.knn = np.array(
[i[1].data for i in heapq.nlargest(near_k, self.knn)])
return self.knn
示例6: top
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def top(self, num):
"""
Get the top N elements from a RDD.
Note: It returns the list sorted in descending order.
>>> sc.parallelize([10, 4, 2, 12, 3]).top(1)
[12]
>>> sc.parallelize([2, 3, 4, 5, 6], 2).cache().top(2)
[6, 5]
"""
def topIterator(iterator):
q = []
for k in iterator:
if len(q) < num:
heapq.heappush(q, k)
else:
heapq.heappushpop(q, k)
yield q
def merge(a, b):
return next(topIterator(a + b))
return sorted(self.mapPartitions(topIterator).reduce(merge), reverse=True)
示例7: topKFrequent
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def topKFrequent(self, nums, K):
"""
Count and Maintain a heap with size k -> O(n lg k)
Since python heapq does not support cmp, need to wrap data in a struct
Need to use min heap instead of max heap, since we need to pop the minimal one
:type nums: List[int]
:type K: int
:rtype: List[int]
"""
cnt = defaultdict(int)
for e in nums:
cnt[e] += 1
lst = []
for k, v in cnt.items():
lst.append(Counter(k, v))
ret = []
for elt in lst:
if len(ret) < K:
heapq.heappush(ret, elt)
else:
heapq.heappushpop(ret, elt)
return map(lambda x: x.val, ret)
示例8: thirdMax
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def thirdMax(self, nums):
"""
It is an easy question but error prone:
1. Choice of min heap or max heap: use min heap (not max heap) because
we want to know the smallest maximum number
2. Duplicate number
:type nums: List[int]
:rtype: int
"""
if not nums:
return None
h = []
for e in set(nums):
if len(h) < 3:
heapq.heappush(h, e)
elif len(h) == 3 and e > h[0]:
heapq.heappushpop(h, e)
assert len(h) <= 3
if len(h) == 3:
ret = min(h)
else:
ret = max(h)
return ret
示例9: data_gen
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def data_gen(self):
time_data_buffer = []
for time_data in self.reader:
# Drop "old" detections
if len(time_data_buffer) >= self.buffer_size and \
time_data < time_data_buffer[0]:
warn('"Old" detection dropped')
continue
# Yield oldest when buffer full
if len(time_data_buffer) >= self.buffer_size:
yield heapq.heappushpop(time_data_buffer, time_data)
else:
# Else just insert
heapq.heappush(time_data_buffer, time_data)
# No more new data: yield remaining buffer
while time_data_buffer:
yield heapq.heappop(time_data_buffer)
示例10: push
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
示例11: shuf_file
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def shuf_file(f, shuf_win):
heap = []
for line in f:
key = hash(line)
if len(heap) < shuf_win:
heapq.heappush(heap, (key, line))
else:
_, out = heapq.heappushpop(heap, (key, line))
yield out
while len(heap) > 0:
_, out = heapq.heappop(heap)
yield out
示例12: k_closest
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def k_closest(points, k, origin=(0, 0)):
# Time: O(k+(n-k)logk)
# Space: O(k)
"""Initialize max heap with first k points.
Python does not support a max heap; thus we can use the default min heap where the keys (distance) are negated.
"""
heap = [(-distance(p, origin), p) for p in points[:k]]
heapify(heap)
"""
For every point p in points[k:],
check if p is smaller than the root of the max heap;
if it is, add p to heap and remove root. Reheapify.
"""
for p in points[k:]:
d = distance(p, origin)
heappushpop(heap, (-d, p)) # heappushpop does conditional check
"""Same as:
if d < -heap[0][0]:
heappush(heap, (-d,p))
heappop(heap)
Note: heappushpop is more efficient than separate push and pop calls.
Each heappushpop call takes O(logk) time.
"""
return [p for nd, p in heap] # return points in heap
示例13: push
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def push(self, x):
"""Pushes a new element."""
assert self._data is not None
if len(self._data) < self._n:
heapq.heappush(self._data, x)
else:
heapq.heappushpop(self._data, x)
示例14: push
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def push(self, item):
if len(self.q) < self.maxlen:
heapq.heappush(self.q, item)
else:
heapq.heappushpop(self.q, item)
示例15: add
# 需要導入模塊: import heapq [as 別名]
# 或者: from heapq import heappushpop [as 別名]
def add(self, item):
if item is None:
return
if len(self._heap) < self._n:
heapq.heappush(self._heap, item)
else:
heapq.heappushpop(self._heap, item)