¡@

Home 

python Programming Glossary: heapq

Python class to merge sorted files, how can this be improved?

http://stackoverflow.com/questions/1001569/python-class-to-merge-sorted-files-how-can-this-be-improved

'w' lines_written 0 previous_comparable '' for line in heapq26.merge decorated_file f keyfunc for f in files comparable line.. share improve this question Note that in python2.6 heapq has a new merge function which will do this for you. To handle.. map open filenames outfile open 'merged.txt' for line in heapq.merge decorated_file f keyfunc for f in files outfile.write..

Python random.sample with a generator

http://stackoverflow.com/questions/12581437/python-random-sample-with-a-generator

sample sizes. The code I used to time the methods from heapq import nlargest import random import timeit def iterSample iterable..

How should I understand the output of dis.dis?

http://stackoverflow.com/questions/12673074/how-should-i-understand-the-output-of-dis-dis

. Here is a very specific example in Python 2.7.3 dis.dis heapq.nsmallest d 3 0 BUILD_SET 24933 3 JUMP_IF_TRUE_OR_POP 11889.. increment by 3 each time but not quite . If I wrap dis.dis heapq.nsmallest d 3 inside a function def f_heapq_nsmallest d n return.. I wrap dis.dis heapq.nsmallest d 3 inside a function def f_heapq_nsmallest d n return heapq.nsmallest d n dis.dis f_heapq d 3..

Finding the Index of N biggest elements in Python Array / List Efficiently

http://stackoverflow.com/questions/12787650/finding-the-index-of-n-biggest-elements-in-python-array-list-efficiently

if is there any efficient library like bottleneck or heapq or maybe a pythonic approach to make this very fast. I have.. sorted range len a key lambda i a i time 230 ms solution 2 heapq.nlargest len a zip a itertools.count time 396 ms solution 3.. len a zip a itertools.count time 396 ms solution 3 heapq.nlargest len a enumerate a key operator.itemgetter 1 time 864..

How can I implement decrease-key functionality in Python's heapq?

http://stackoverflow.com/questions/1465662/how-can-i-implement-decrease-key-functionality-in-pythons-heapq

can I implement decrease key functionality in Python's heapq I know it is possible to realize decrease key functionality.. element with a child until heap condition is restore . In heapq.py that's called _siftdown and similarly _siftup for INcrementing.. instead of O log N sifting or reimplement some or all of heapq's functionality to make the sifting primitives exposed as public..

how to efficiently get the k bigger elements of a list in python

http://stackoverflow.com/questions/2243542/how-to-efficiently-get-the-k-bigger-elements-of-a-list-in-python

python share improve this question Use nlargest from heapq module from heapq import nlargest lst 9 1 6 4 2 8 3 7 5 nlargest.. this question Use nlargest from heapq module from heapq import nlargest lst 9 1 6 4 2 8 3 7 5 nlargest 3 lst # Gives.. to nlargest in case you wanna change your criteria from heapq import nlargest tags python 30 ruby 25 c 50 lisp 20 nlargest..

What do I use for a max-heap implementation in Python?

http://stackoverflow.com/questions/2501457/what-do-i-use-for-a-max-heap-implementation-in-python

a max heap implementation in Python Python includes the heapq module for min heaps but I need a max heap. What should I use.. The easiest way is to invert the value of the keys and use heapq. For example turn 1000.0 into 1000.0 and 5.0 into 5.0. share..

Return a list of imported Python modules used in a script?

http://stackoverflow.com/questions/2572582/return-a-list-of-imported-python-modules-used-in-a-script

returned from the ModuleFinder script return tokenize heapq __future__ copy_reg sre_compile _collections cStringIO _sre.. subprocess cmd gc __main__ operator array select _heapq _threading_local abc _bisect posixpath _random os2emxpath tempfile..

Creating a python priority Queue

http://stackoverflow.com/questions/3311480/creating-a-python-priority-queue

queue task queue share improve this question Use the heapq module in the standard library. You don't specify how you wanted.. dictionaries but here's a simple implementation import heapq class MyPriQueue object def __init__ self self.heap def add.. object def __init__ self self.heap def add self d pri heapq.heappush self.heap pri d def get self pri d heapq.heappop self.heap..

Find the oldest file (recursively) in a directory

http://stackoverflow.com/questions/837606/find-the-oldest-file-recursively-in-a-directory

get the n oldest files similar to Nadia's answer import os heapq def oldest_files_in_tree rootfolder count 1 extension .avi return.. rootfolder count 1 extension .avi return heapq.nsmallest count os.path.join dirname filename for dirname dirnames..

Joining a set of ordered-integer yielding Python iterators

http://stackoverflow.com/questions/969709/joining-a-set-of-ordered-integer-yielding-python-iterators

iterator generator share improve this question import heapq itertools def intersect its for key values in itertools.groupby.. def intersect its for key values in itertools.groupby heapq.merge its if len list values len its yield key list intersect..

How to implement Priority Queues in Python?

http://stackoverflow.com/questions/9969236/how-to-implement-priority-queues-in-python

0 Link 2 Heap Implementation http docs.python.org library heapq.html Here they says that we can implement priority queues indirectly.. that we can implement priority queues indirectly using heapq pq # list of entries arranged in a heap entry_finder # mapping.. The version in the Queue module is implemented using the heapq module so they have equal efficiency for the underlying heap..