From aefde435efb8e99c74ad147e1a1db76fe5de61aa Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 15 Jun 2004 23:53:35 +0000 Subject: Reverse argument order for nsmallest() and nlargest(). Reads better when the iterable is a generator expression. --- Doc/lib/libheapq.tex | 4 ++-- Lib/difflib.py | 2 +- Lib/test/test_heapq.py | 8 ++++---- Modules/_heapqmodule.c | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Doc/lib/libheapq.tex b/Doc/lib/libheapq.tex index 5a140ab..ffcafcd 100644 --- a/Doc/lib/libheapq.tex +++ b/Doc/lib/libheapq.tex @@ -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} diff --git a/Lib/difflib.py b/Lib/difflib.py index 85a1c9c..529c786 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -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] diff --git a/Lib/test/test_heapq.py b/Lib/test/test_heapq.py index b6fec9f..4e535e9 100644 --- a/Lib/test/test_heapq.py +++ b/Lib/test/test_heapq.py @@ -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] diff --git a/Modules/_heapqmodule.c b/Modules/_heapqmodule.c index 8074bb1..21df796 100644 --- a/Modules/_heapqmodule.c +++ b/Modules/_heapqmodule.c @@ -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); -- cgit v0.12