In Python, how do you get the position of an item in a list (using list.index
) using fuzzy matching?
For example, how do I get the indexes of all fruit of t
With regular expressions:
import re
fruit_list = ['raspberry', 'apple', 'strawberry']
berry_idx = [i for i, item in enumerate(fruit_list) if re.search('berry$', item)]
And without regular expressions:
fruit_list = ['raspberry', 'apple', 'strawberry']
berry_idx = [i for i, item in enumerate(fruit_list) if item.endswith('berry')]