Created
December 21, 2013 09:57
-
-
Save ourway/8067495 to your computer and use it in GitHub Desktop.
Efficient ways of searching sorted lists.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def bi_contains(lst, item): | |
| """The most efficient way of searching sorted lists. | |
| @ref: http://goo.gl/ByYtjq | |
| @param lst: The input list | |
| @param item: The item i'm looking for | |
| @return: True|False | |
| """ | |
| return (item <= lst[-1]) and (lst[bisect_left(lst, item)] == item) | |
| if __name__ == '__main__': | |
| ''' Test Lines ''' | |
| a = [(1,2), (1,3), (1,4), (2,2)] | |
| x = (1,5) | |
| print bi_contains(a, x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment