Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb6b734

Browse files
committed
Apply heapq.nlargest() to find best matches.
1 parent 47edb4b commit bb6b734

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

Lib/difflib.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
'Differ','IS_CHARACTER_JUNK', 'IS_LINE_JUNK', 'context_diff',
3030
'unified_diff']
3131

32+
import heapq
33+
3234
def _calculate_ratio(matches, length):
3335
if length:
3436
return 2.0 * matches / length
@@ -703,9 +705,9 @@ def get_close_matches(word, possibilities, n=3, cutoff=0.6):
703705
result.append((s.ratio(), x))
704706

705707
# Move the best scorers to head of list
706-
result.sort(reverse=True)
708+
result = heapq.nlargest(result, n)
707709
# Strip scores for the best n matches
708-
return [x for score, x in result[:n]]
710+
return [x for score, x in result]
709711

710712
def _count_leading(line, ch):
711713
"""

0 commit comments

Comments
 (0)