Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
4901a1f2
Kaydet (Commit)
4901a1f2
authored
Ara 02, 2004
tarafından
Raymond Hettinger
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add key= argument to heapq.nsmallest() and heapq.nlargest().
üst
de7b9904
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
90 additions
and
8 deletions
+90
-8
libheapq.tex
Doc/lib/libheapq.tex
+14
-6
heapq.py
Lib/heapq.py
+29
-1
test_heapq.py
Lib/test/test_heapq.py
+7
-1
NEWS
Misc/NEWS
+40
-0
No files found.
Doc/lib/libheapq.tex
Dosyayı görüntüle @
4901a1f2
...
...
@@ -90,16 +90,24 @@ True
The module also offers two general purpose functions based on heaps.
\begin{funcdesc}
{
nlargest
}{
n, iterable
}
\begin{funcdesc}
{
nlargest
}{
n, iterable
\optional
{
, key
}
}
Return a list with the
\var
{
n
}
largest elements from the dataset defined
by
\var
{
iterable
}
. Equivalent to:
\code
{
sorted(iterable, reverse=True)[:n]
}
\versionadded
{
2.4
}
by
\var
{
iterable
}
.
\var
{
key
}
, if provided, specifies a function of one
argument that is used to extract a comparison key from each element
in the iterable:
\samp
{
\var
{
key
}
=
\function
{
str.lower
}}
Equivalent to:
\samp
{
sorted(iterable, key=key, reverse=True)[:n]
}
\versionadded
{
2.4
}
\versionchanged
[Added the optional \var{key} argument]
{
2.5
}
\end{funcdesc}
\begin{funcdesc}
{
nsmallest
}{
n, iterable
}
\begin{funcdesc}
{
nsmallest
}{
n, iterable
\optional
{
, key
}
}
Return a list with the
\var
{
n
}
smallest elements from the dataset defined
by
\var
{
iterable
}
. Equivalent to:
\code
{
sorted(iterable)[:n]
}
\versionadded
{
2.4
}
by
\var
{
iterable
}
.
\var
{
key
}
, if provided, specifies a function of one
argument that is used to extract a comparison key from each element
in the iterable:
\samp
{
\var
{
key
}
=
\function
{
str.lower
}}
Equivalent to:
\samp
{
sorted(iterable, key=key)[:n]
}
\versionadded
{
2.4
}
\versionchanged
[Added the optional \var{key} argument]
{
2.5
}
\end{funcdesc}
Both functions perform best for smaller values of
\var
{
n
}
. For larger
...
...
Lib/heapq.py
Dosyayı görüntüle @
4901a1f2
...
...
@@ -129,7 +129,8 @@ From all times, sorting has always been a Great Art! :-)
__all__
=
[
'heappush'
,
'heappop'
,
'heapify'
,
'heapreplace'
,
'nlargest'
,
'nsmallest'
]
from
itertools
import
islice
,
repeat
from
itertools
import
islice
,
repeat
,
count
,
imap
,
izip
,
tee
from
operator
import
itemgetter
import
bisect
def
heappush
(
heap
,
item
):
...
...
@@ -307,6 +308,33 @@ try:
except
ImportError
:
pass
# Extend the implementations of nsmallest and nlargest to use a key= argument
_nsmallest
=
nsmallest
def
nsmallest
(
n
,
iterable
,
key
=
None
):
"""Find the n smallest elements in a dataset.
Equivalent to: sorted(iterable, key=key)[:n]
"""
if
key
is
None
:
return
_nsmallest
(
n
,
iterable
)
in1
,
in2
=
tee
(
iterable
)
it
=
izip
(
imap
(
key
,
in1
),
count
(),
in2
)
# decorate
result
=
_nsmallest
(
n
,
it
)
return
map
(
itemgetter
(
2
),
result
)
# undecorate
_nlargest
=
nlargest
def
nlargest
(
n
,
iterable
,
key
=
None
):
"""Find the n largest elements in a dataset.
Equivalent to: sorted(iterable, key=key, reverse=True)[:n]
"""
if
key
is
None
:
return
_nlargest
(
n
,
iterable
)
in1
,
in2
=
tee
(
iterable
)
it
=
izip
(
imap
(
key
,
in1
),
count
(),
in2
)
# decorate
result
=
_nlargest
(
n
,
it
)
return
map
(
itemgetter
(
2
),
result
)
# undecorate
if
__name__
==
"__main__"
:
# Simple sanity test
heap
=
[]
...
...
Lib/test/test_heapq.py
Dosyayı görüntüle @
4901a1f2
...
...
@@ -105,13 +105,19 @@ class TestHeap(unittest.TestCase):
def
test_nsmallest
(
self
):
data
=
[
random
.
randrange
(
2000
)
for
i
in
range
(
1000
)]
f
=
lambda
x
:
x
*
547
%
2000
for
n
in
(
0
,
1
,
2
,
10
,
100
,
400
,
999
,
1000
,
1100
):
self
.
assertEqual
(
nsmallest
(
n
,
data
),
sorted
(
data
)[:
n
])
self
.
assertEqual
(
nsmallest
(
n
,
data
,
key
=
f
),
sorted
(
data
,
key
=
f
)[:
n
])
def
test_largest
(
self
):
def
test_
n
largest
(
self
):
data
=
[
random
.
randrange
(
2000
)
for
i
in
range
(
1000
)]
f
=
lambda
x
:
x
*
547
%
2000
for
n
in
(
0
,
1
,
2
,
10
,
100
,
400
,
999
,
1000
,
1100
):
self
.
assertEqual
(
nlargest
(
n
,
data
),
sorted
(
data
,
reverse
=
True
)[:
n
])
self
.
assertEqual
(
nlargest
(
n
,
data
,
key
=
f
),
sorted
(
data
,
key
=
f
,
reverse
=
True
)[:
n
])
#==============================================================================
...
...
Misc/NEWS
Dosyayı görüntüle @
4901a1f2
...
...
@@ -4,6 +4,46 @@ Python News
(
editors
:
check
NEWS
.
help
for
information
about
editing
NEWS
using
ReST
.)
What
's New in Python 2.5 alpha 1?
=================================
Core and builtins
-----------------
Extension Modules
-----------------
Library
-------
- heapq.nsmallest() and heapq.nlargest() now support key= arguments with
the same meaning as for list.sort().
Build
-----
C API
-----
Tests
-----
Mac
---
Tools/Demos
-----------
What'
s
New
in
Python
2.4
final
?
===============================
...
...
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