1# There is several possible ways if "finding" things in lists.
2'Checking if something is inside'
33 in [1, 2, 3] # => True
4'Filtering a collection'
5matches = [x for x in lst if fulfills_some_condition(x)]
6matches = filter(fulfills_some_condition, lst)
7matches = (x for x in lst if x > 6)
8'Finding the first occurrence'
9next(x for x in lst if ...)
10next((x for x in lst if ...), [default value])
11'Finding the location of an item'
12[1,2,3].index(2) # => 1
13[1,2,3,2].index(2) # => 1
14[1,2,3].index(4) # => ValueError
15[i for i,x in enumerate([1,2,3,2]) if x==2] # => [1, 3]
1
2def find_all_indexes(input_str, search_str):
3 l1 = []
4 length = len(input_str)
5 index = 0
6 while index < length:
7 i = input_str.find(search_str, index)
8 if i == -1:
9 return l1
10 l1.append(i)
11 index = i + 1
12 return l1
13
14
15s = 'abaacdaa12aa2'
16print(find_all_indexes(s, 'a'))
17print(find_all_indexes(s, 'aa'))
18
1def find_all_indexes2(input_str, search_str):
2 indexes = []
3 index = input_str.find(search_str)
4
5 while index != -1:
6 indexes.append(index)
7 index = input_str.find(search_str, index+1)
8
9 return indexes
10
11print(find_all_indexes2("Can you can a can as a canner can can a can", "can"))
12# returns [8, 14, 23, 30, 34, 40]
13# Note: Capital "Can" was not found