chainsol

Untitled

Dec 22nd, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. list_to_search = ["Birding", "Large Bird Handling"]
  2. search_key = "Bird"
  3. # first list comprehension will match *any* location
  4. # case-sensitive
  5. result_any_position_case_sensitive = [item for item in list_to_search if search_key in item]
  6. # case-insensitive
  7. result_any_position = [item for item in list_to_search if search_key.lower() in item.lower()]
  8. # second list comprehension will match only at the beginning of each word
  9. # regex is the better solution for this
  10. # case sensitive
  11. result_beginning_of_word_case_sensitive = [item for item in list_to_search if item.startswith(search_key)]
  12. # case insensitive
  13. result_beginning_of_word = [item for item in list_to_search if item.lower().startswith(search_key.lower())]
Add Comment
Please, Sign In to add comment