diff --git a/Doc/library/bisect.rst b/Doc/library/bisect.rst index 6bf7814..374ba81 100644 --- a/Doc/library/bisect.rst +++ b/Doc/library/bisect.rst @@ -120,6 +120,7 @@ example uses :func:`bisect` to look up a letter grade for an exam score (say) based on a set of ordered numeric breakpoints: 90 and up is an 'A', 80 to 89 is a 'B', and so on:: + >>> from bisect import bisect >>> def grade(score, breakpoints=[60, 70, 80, 90], grades='FDCBA'): ... i = bisect(breakpoints, score) ... return grades[i] @@ -135,6 +136,7 @@ all of the previous key lookups). Instead, it is better to search a list of precomputed keys to find the index of the record in question:: + >>> from bisect import bisect_left >>> data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)] >>> data.sort(key=lambda r: r[1]) >>> keys = [r[1] for r in data] # precomputed list of keys