Reverse argument order for nsmallest() and nlargest().
Reads better when the iterable is a generator expression.
This commit is contained in:
parent
969297f488
commit
aefde435ef
|
@ -85,13 +85,13 @@ True
|
|||
|
||||
The module also offers two general purpose functions based on heaps.
|
||||
|
||||
\begin{funcdesc}{nlargest}{iterable, n}
|
||||
\begin{funcdesc}{nlargest}{n, iterable}
|
||||
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}
|
||||
\begin{funcdesc}{nsmallest}{n, iterable}
|
||||
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}
|
||||
|
|
|
@ -705,7 +705,7 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6):
|
|||
result.append((s.ratio(), x))
|
||||
|
||||
# Move the best scorers to head of list
|
||||
result = heapq.nlargest(result, n)
|
||||
result = heapq.nlargest(n, result)
|
||||
# Strip scores for the best n matches
|
||||
return [x for score, x in result]
|
||||
|
||||
|
|
|
@ -92,13 +92,13 @@ class TestHeap(unittest.TestCase):
|
|||
|
||||
def test_nsmallest(self):
|
||||
data = [random.randrange(2000) for i in range(1000)]
|
||||
for i in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
|
||||
self.assertEqual(nsmallest(data, i), sorted(data)[:i])
|
||||
for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
|
||||
self.assertEqual(nsmallest(n, data), sorted(data)[:n])
|
||||
|
||||
def test_largest(self):
|
||||
data = [random.randrange(2000) for i in range(1000)]
|
||||
for i in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
|
||||
self.assertEqual(nlargest(data, i), sorted(data, reverse=True)[:i])
|
||||
for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
|
||||
self.assertEqual(nlargest(n, data), sorted(data, reverse=True)[:n])
|
||||
|
||||
def test_main(verbose=None):
|
||||
test_classes = [TestHeap]
|
||||
|
|
|
@ -222,7 +222,7 @@ nlargest(PyObject *self, PyObject *args)
|
|||
PyObject *heap=NULL, *elem, *iterable, *sol, *it, *oldelem;
|
||||
int i, n;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "Oi:nlargest", &iterable, &n))
|
||||
if (!PyArg_ParseTuple(args, "iO:nlargest", &n, &iterable))
|
||||
return NULL;
|
||||
|
||||
it = PyObject_GetIter(iterable);
|
||||
|
@ -381,7 +381,7 @@ nsmallest(PyObject *self, PyObject *args)
|
|||
PyObject *heap=NULL, *elem, *iterable, *los, *it, *oldelem;
|
||||
int i, n;
|
||||
|
||||
if (!PyArg_ParseTuple(args, "Oi:nsmallest", &iterable, &n))
|
||||
if (!PyArg_ParseTuple(args, "iO:nsmallest", &n, &iterable))
|
||||
return NULL;
|
||||
|
||||
it = PyObject_GetIter(iterable);
|
||||
|
|
Loading…
Reference in New Issue