(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/module-array.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/module-bisect.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/module-bisect.html| ]]
|
Python Library Reference
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/contents.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/modindex.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/genindex.html| ]]
|
Next: 5.6 array Up: 5.5 bisect Previous: 5.5 bisect
5.5.1 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']
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/module-array.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/module-bisect.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/module-bisect.html| ]]
|
Python Library Reference
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/contents.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/modindex.html| ]]
|
[[yurttas/PL/SL/python/docs/core-python-programming/doc/152/lib/genindex.html| ]]
|
Next: 5.6 array Up: 5.5 bisect Previous: 5.5 bisect
Send comments on this document to python-docs@python.org.