Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
00166c55
Commit
00166c55
authored
Feb 19, 2007
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add merge() function to heapq.
parent
d6fc72a5
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
64 additions
and
5 deletions
+64
-5
Doc/lib/libheapq.tex
Doc/lib/libheapq.tex
+13
-2
Lib/heapq.py
Lib/heapq.py
+40
-2
Lib/test/test_heapq.py
Lib/test/test_heapq.py
+9
-1
Misc/NEWS
Misc/NEWS
+2
-0
No files found.
Doc/lib/libheapq.tex
View file @
00166c55
...
...
@@ -88,7 +88,18 @@ True
>>>
\end{verbatim}
The module also offers two general purpose functions based on heaps.
The module also offers three general purpose functions based on heaps.
\begin{funcdesc}
{
merge
}{
*iterables
}
Merge multiple sorted inputs into a single sorted output (for example, merge
timestamped entries from multiple log files). Returns an iterator over
over the sorted values.
Similar to
\code
{
sorted(itertools.chain(*iterables))
}
but returns an iterable,
does not pull the data into memory all at once, and reduces the number of
comparisons by assuming that each of the input streams is already sorted.
\versionadded
{
2.6
}
\end{funcdesc}
\begin{funcdesc}
{
nlargest
}{
n, iterable
\optional
{
, key
}}
Return a list with the
\var
{
n
}
largest elements from the dataset defined
...
...
@@ -110,7 +121,7 @@ Equivalent to: \samp{sorted(iterable, key=key)[:n]}
\versionchanged
[Added the optional \var{key} argument]
{
2.5
}
\end{funcdesc}
Both
functions perform best for smaller values of
\var
{
n
}
. For larger
The latter two
functions perform best for smaller values of
\var
{
n
}
. For larger
values, it is more efficient to use the
\function
{
sorted()
}
function. Also,
when
\code
{
n==1
}
, it is more efficient to use the builtin
\function
{
min()
}
and
\function
{
max()
}
functions.
...
...
Lib/heapq.py
View file @
00166c55
...
...
@@ -126,8 +126,8 @@ Believe me, real good tape sorts were quite spectacular to watch!
From all times, sorting has always been a Great Art! :-)
"""
__all__
=
[
'heappush'
,
'heappop'
,
'heapify'
,
'heapreplace'
,
'
nlargest
'
,
'nsmallest'
]
__all__
=
[
'heappush'
,
'heappop'
,
'heapify'
,
'heapreplace'
,
'
merge
'
,
'n
largest'
,
'n
smallest'
]
from
itertools
import
islice
,
repeat
,
count
,
imap
,
izip
,
tee
from
operator
import
itemgetter
,
neg
...
...
@@ -308,6 +308,41 @@ try:
except
ImportError
:
pass
def
merge
(
*
iterables
):
'''Merge multiple sorted inputs into a single sorted output.
Similar to sorted(itertools.chain(*iterables)) but returns an iterable,
does not pull the data into memory all at once, and reduces the number
of comparisons by assuming that each of the input streams is already sorted.
>>> list(merge([1,3,5,7], [0,2,4,8], [5,10,15,20], [], [25]))
[0, 1, 2, 3, 4, 5, 5, 7, 8, 10, 15, 20, 25]
'''
_heappop
,
siftup
,
_StopIteration
=
heappop
,
_siftup
,
StopIteration
h
=
[]
h_append
=
h
.
append
for
it
in
map
(
iter
,
iterables
):
try
:
next
=
it
.
next
h_append
([
next
(),
next
])
except
_StopIteration
:
pass
heapify
(
h
)
while
1
:
try
:
while
1
:
v
,
next
=
s
=
h
[
0
]
# raises IndexError when h is empty
yield
v
s
[
0
]
=
next
()
# raises StopIteration when exhausted
siftup
(
h
,
0
)
# restore heap condition
except
_StopIteration
:
_heappop
(
h
)
# remove empty iterator
except
IndexError
:
return
# Extend the implementations of nsmallest and nlargest to use a key= argument
_nsmallest
=
nsmallest
def
nsmallest
(
n
,
iterable
,
key
=
None
):
...
...
@@ -341,3 +376,6 @@ if __name__ == "__main__":
while
heap
:
sort
.
append
(
heappop
(
heap
))
print
sort
import
doctest
doctest
.
testmod
()
Lib/test/test_heapq.py
View file @
00166c55
"""Unittests for heapq."""
from
heapq
import
heappush
,
heappop
,
heapify
,
heapreplace
,
nlargest
,
nsmallest
from
heapq
import
heappush
,
heappop
,
heapify
,
heapreplace
,
merge
,
nlargest
,
nsmallest
import
random
import
unittest
from
test
import
test_support
...
...
@@ -103,6 +103,14 @@ class TestHeap(unittest.TestCase):
heap_sorted
=
[
heappop
(
heap
)
for
i
in
range
(
size
)]
self
.
assertEqual
(
heap_sorted
,
sorted
(
data
))
def
test_merge
(
self
):
inputs
=
[]
for
i
in
xrange
(
random
.
randrange
(
5
)):
row
=
sorted
(
random
.
randrange
(
1000
)
for
j
in
range
(
random
.
randrange
(
10
)))
inputs
.
append
(
row
)
self
.
assertEqual
(
sorted
(
chain
(
*
inputs
)),
list
(
merge
(
*
inputs
)))
self
.
assertEqual
(
list
(
merge
()),
[])
def
test_nsmallest
(
self
):
data
=
[(
random
.
randrange
(
2000
),
i
)
for
i
in
range
(
1000
)]
for
f
in
(
None
,
lambda
x
:
x
[
0
]
*
547
%
2000
):
...
...
Misc/NEWS
View file @
00166c55
...
...
@@ -128,6 +128,8 @@ Core and builtins
Library
-------
- Added heapq.merge() for merging sorted input streams.
- Have the encoding package'
s
search
function
dynamically
import
using
absolute
import
semantics
.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment