Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aefde43

Browse files
committed
Reverse argument order for nsmallest() and nlargest().
Reads better when the iterable is a generator expression.
1 parent 969297f commit aefde43

4 files changed

Lines changed: 9 additions & 9 deletions

File tree

Doc/lib/libheapq.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,13 @@ \section{\module{heapq} ---
8585

8686
The module also offers two general purpose functions based on heaps.
8787

88-
\begin{funcdesc}{nlargest}{iterable, n}
88+
\begin{funcdesc}{nlargest}{n, iterable}
8989
Return a list with the \var{n} largest elements from the dataset defined
9090
by \var{iterable}. Equivalent to: \code{sorted(iterable, reverse=True)[:n]}
9191
\versionadded{2.4}
9292
\end{funcdesc}
9393

94-
\begin{funcdesc}{nsmallest}{iterable, n}
94+
\begin{funcdesc}{nsmallest}{n, iterable}
9595
Return a list with the \var{n} smallest elements from the dataset defined
9696
by \var{iterable}. Equivalent to: \code{sorted(iterable)[:n]}
9797
\versionadded{2.4}

Lib/difflib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6):
705705
result.append((s.ratio(), x))
706706

707707
# Move the best scorers to head of list
708-
result = heapq.nlargest(result, n)
708+
result = heapq.nlargest(n, result)
709709
# Strip scores for the best n matches
710710
return [x for score, x in result]
711711

Lib/test/test_heapq.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ def test_heapsort(self):
9292

9393
def test_nsmallest(self):
9494
data = [random.randrange(2000) for i in range(1000)]
95-
for i in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
96-
self.assertEqual(nsmallest(data, i), sorted(data)[:i])
95+
for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
96+
self.assertEqual(nsmallest(n, data), sorted(data)[:n])
9797

9898
def test_largest(self):
9999
data = [random.randrange(2000) for i in range(1000)]
100-
for i in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
101-
self.assertEqual(nlargest(data, i), sorted(data, reverse=True)[:i])
100+
for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
101+
self.assertEqual(nlargest(n, data), sorted(data, reverse=True)[:n])
102102

103103
def test_main(verbose=None):
104104
test_classes = [TestHeap]

Modules/_heapqmodule.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ nlargest(PyObject *self, PyObject *args)
222222
PyObject *heap=NULL, *elem, *iterable, *sol, *it, *oldelem;
223223
int i, n;
224224

225-
if (!PyArg_ParseTuple(args, "Oi:nlargest", &iterable, &n))
225+
if (!PyArg_ParseTuple(args, "iO:nlargest", &n, &iterable))
226226
return NULL;
227227

228228
it = PyObject_GetIter(iterable);
@@ -381,7 +381,7 @@ nsmallest(PyObject *self, PyObject *args)
381381
PyObject *heap=NULL, *elem, *iterable, *los, *it, *oldelem;
382382
int i, n;
383383

384-
if (!PyArg_ParseTuple(args, "Oi:nsmallest", &iterable, &n))
384+
if (!PyArg_ParseTuple(args, "iO:nsmallest", &n, &iterable))
385385
return NULL;
386386

387387
it = PyObject_GetIter(iterable);

0 commit comments

Comments
 (0)