I want to search a sorted list of strings for all of the elements that start with a given substring.
Here's an example that finds all of the exact matches:
import bisect
names = ['adam', 'bob', 'bob', 'bob', 'bobby', 'bobert', 'chris']
names.sort()
leftIndex = bisect.bisect_left(names, 'bob')
rightIndex = bisect.bisect_right(names, 'bob')
print(names[leftIndex:rightIndex])
Which prints ['bob', 'bob', 'bob'].
Instead, I want to search for all the names that start with 'bob'. The output I want is ['bob', 'bob', 'bob', 'bobby', 'bobert']. If I could modify the comparison method of the bisect search, then I could use name.startswith('bob') to do this.
As an example, in Java it would be easy. I would use:
Arrays.binarySearch(names, "bob", myCustomComparator);
where 'myCustomComparator' is a comparator that takes advantage of the startswith method (and some additional logic).
How do I do this in Python?