diff Lib/difflib.py @ 32339:408a40e65247 legacy-trunk

Apply heapq.nlargest() to find best matches.
author Raymond Hettinger <python@rcn.com>
date Sun, 13 Jun 2004 09:57:33 +0000
parents 71a294bd7950
children a46bde9e0c74
line wrap: on
line diff
--- a/Lib/difflib.py	Sun Jun 13 08:20:46 2004 +0000
+++ b/Lib/difflib.py	Sun Jun 13 09:57:33 2004 +0000
@@ -29,6 +29,8 @@
            'Differ','IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff',
            'unified_diff']
 
+import heapq
+
 def _calculate_ratio(matches, length):
     if length:
         return 2.0 * matches / length
@@ -703,9 +705,9 @@
             result.append((s.ratio(), x))
 
     # Move the best scorers to head of list
-    result.sort(reverse=True)
+    result = heapq.nlargest(result, n)
     # Strip scores for the best n matches
-    return [x for score, x in result[:n]]
+    return [x for score, x in result]
 
 def _count_leading(line, ch):
     """