GNU Info

Info Node: (python2.1-lib.info)bisect-example

(python2.1-lib.info)bisect-example


Prev: bisect Up: bisect
Enter node , (file) or (file)node

Example
-------

The `bisect()' function is generally useful for categorizing numeric
data.  This example uses `bisect()' to look up a letter grade for an
exam total (say) based on a set of ordered numeric breakpoints: 85 and
up is an `A', 75..84 is a `B', etc.

     >>> grades = "FEDCBA"
     >>> breakpoints = [30, 44, 66, 75, 85]
     >>> from bisect import bisect
     >>> def grade(total):
     ...           return grades[bisect(breakpoints, total)]
     ...
     >>> grade(66)
     'C'
     >>> map(grade, [33, 99, 77, 44, 12, 88])
     ['E', 'A', 'B', 'D', 'F', 'A']


automatically generated by info2www version 1.2.2.9