Add example of how to do key lookups with bisect().

This commit is contained in:
Raymond Hettinger 2009-06-11 22:08:48 +00:00
parent 16e527fc1a
commit 75b544886b
1 changed files with 2 additions and 2 deletions

View File

@ -94,8 +94,8 @@ Instead, it is better to search a list of precomputed keys to find the index
of the record in question::
>>> data = [('red', 5), ('blue', 1), ('yellow', 8), ('black', 0)]
>>> data.sort(key=lambda r: r[1]) # precomputed list of keys
>>> keys = [r[1] for r in data]
>>> data.sort(key=lambda r: r[1])
>>> keys = [r[1] for r in data] # precomputed list of keys
>>> data[bisect_left(keys, 0)]
('black', 0)
>>> data[bisect_left(keys, 1)]