Add example of how to do key lookups with bisect().
This commit is contained in:
parent
16e527fc1a
commit
75b544886b
|
@ -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)]
|
||||
|
|
Loading…
Reference in New Issue