diff options
author | Raymond Hettinger <python@rcn.com> | 2004-06-13 09:57:33 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-06-13 09:57:33 (GMT) |
commit | bb6b7346cedbabb9cb0d2ad283fb598a2c8d9b72 (patch) | |
tree | b7673662682844280987988b6f4c2dc278200f3c | |
parent | 47edb4b09ce4694e5585497581a1a0e7e047e393 (diff) | |
download | cpython-bb6b7346cedbabb9cb0d2ad283fb598a2c8d9b72.zip cpython-bb6b7346cedbabb9cb0d2ad283fb598a2c8d9b72.tar.gz cpython-bb6b7346cedbabb9cb0d2ad283fb598a2c8d9b72.tar.bz2 |
Apply heapq.nlargest() to find best matches.
-rw-r--r-- | Lib/difflib.py | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/Lib/difflib.py b/Lib/difflib.py index c074b19..85a1c9c 100644 --- a/Lib/difflib.py +++ b/Lib/difflib.py @@ -29,6 +29,8 @@ __all__ = ['get_close_matches', 'ndiff', 'restore', 'SequenceMatcher', '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 @@ 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.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): """ |