summaryrefslogtreecommitdiff
path: root/Doc/lib/libheapq.tex
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-06-10 05:03:17 +0000
committerRaymond Hettinger <python@rcn.com>2004-06-10 05:03:17 +0000
commit33ecffb65ae43ece95e4d828f95819395187d579 (patch)
tree499adce5b4fc964973a8e72baf2c6214bcef89e3 /Doc/lib/libheapq.tex
parent7d019664d7fcd3692eafef668fbc2e17126dee14 (diff)
downloadcpython-git-33ecffb65ae43ece95e4d828f95819395187d579.tar.gz
SF patch #969791: Add nlargest() and nsmallest() to heapq.
Diffstat (limited to 'Doc/lib/libheapq.tex')
-rw-r--r--Doc/lib/libheapq.tex24
1 files changed, 24 insertions, 0 deletions
diff --git a/Doc/lib/libheapq.tex b/Doc/lib/libheapq.tex
index 38f9b1a441..4585058b32 100644
--- a/Doc/lib/libheapq.tex
+++ b/Doc/lib/libheapq.tex
@@ -83,6 +83,30 @@ True
>>>
\end{verbatim}
+The module also offers two general purpose functions based on heaps.
+
+\begin{funcdesc}{nlargest}{iterable, n}
+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}
+\end{funcdesc}
+
+\begin{funcdesc}{nsmallest}{iterable, n}
+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}
+\end{funcdesc}
+
+Though the above functions appear symmetrical, they each have different
+speed and space requirements. In particular, \function{nsmallest()}
+operates on a full copy of the dataset. In contrast, \function{nlargest()}
+only requires storage space for \var{n} elements.
+
+Both 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.
+
\subsection{Theory}