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.
|
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
|
Return a list with the \var{n} largest elements from the dataset defined
|
||||||
by \var{iterable}. Equivalent to: \code{sorted(iterable, reverse=True)[:n]}
|
by \var{iterable}. Equivalent to: \code{sorted(iterable, reverse=True)[:n]}
|
||||||
\versionadded{2.4}
|
\versionadded{2.4}
|
||||||
\end{funcdesc}
|
\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
|
Return a list with the \var{n} smallest elements from the dataset defined
|
||||||
by \var{iterable}. Equivalent to: \code{sorted(iterable)[:n]}
|
by \var{iterable}. Equivalent to: \code{sorted(iterable)[:n]}
|
||||||
\versionadded{2.4}
|
\versionadded{2.4}
|
||||||
|
|
|
@ -705,7 +705,7 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6):
|
||||||
result.append((s.ratio(), x))
|
result.append((s.ratio(), x))
|
||||||
|
|
||||||
# Move the best scorers to head of list
|
# 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
|
# Strip scores for the best n matches
|
||||||
return [x for score, x in result]
|
return [x for score, x in result]
|
||||||
|
|
||||||
|
|
|
@ -92,13 +92,13 @@ class TestHeap(unittest.TestCase):
|
||||||
|
|
||||||
def test_nsmallest(self):
|
def test_nsmallest(self):
|
||||||
data = [random.randrange(2000) for i in range(1000)]
|
data = [random.randrange(2000) for i in range(1000)]
|
||||||
for i in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
|
for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
|
||||||
self.assertEqual(nsmallest(data, i), sorted(data)[:i])
|
self.assertEqual(nsmallest(n, data), sorted(data)[:n])
|
||||||
|
|
||||||
def test_largest(self):
|
def test_largest(self):
|
||||||
data = [random.randrange(2000) for i in range(1000)]
|
data = [random.randrange(2000) for i in range(1000)]
|
||||||
for i in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
|
for n in (0, 1, 2, 10, 100, 400, 999, 1000, 1100):
|
||||||
self.assertEqual(nlargest(data, i), sorted(data, reverse=True)[:i])
|
self.assertEqual(nlargest(n, data), sorted(data, reverse=True)[:n])
|
||||||
|
|
||||||
def test_main(verbose=None):
|
def test_main(verbose=None):
|
||||||
test_classes = [TestHeap]
|
test_classes = [TestHeap]
|
||||||
|
|
|
@ -222,7 +222,7 @@ nlargest(PyObject *self, PyObject *args)
|
||||||
PyObject *heap=NULL, *elem, *iterable, *sol, *it, *oldelem;
|
PyObject *heap=NULL, *elem, *iterable, *sol, *it, *oldelem;
|
||||||
int i, n;
|
int i, n;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "Oi:nlargest", &iterable, &n))
|
if (!PyArg_ParseTuple(args, "iO:nlargest", &n, &iterable))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
it = PyObject_GetIter(iterable);
|
it = PyObject_GetIter(iterable);
|
||||||
|
@ -381,7 +381,7 @@ nsmallest(PyObject *self, PyObject *args)
|
||||||
PyObject *heap=NULL, *elem, *iterable, *los, *it, *oldelem;
|
PyObject *heap=NULL, *elem, *iterable, *los, *it, *oldelem;
|
||||||
int i, n;
|
int i, n;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "Oi:nsmallest", &iterable, &n))
|
if (!PyArg_ParseTuple(args, "iO:nsmallest", &n, &iterable))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
it = PyObject_GetIter(iterable);
|
it = PyObject_GetIter(iterable);
|
||||||
|
|
Loading…
Reference in New Issue