I am iterating through a for loop looking for keyword matches in a list and then compiling the match indices to a third list. I can compile the indices as a list of lists, but I want to further group sub-lists by the item they matched.
import re, itertools
my_list = ['ab','cde']
keywords = ['ab','cd','de']
indices=[]
pats = [re.compile(i) for i in keywords]
for pat in pats:
for i in my_list:
for m in re.finditer(pat, i):
a =list((m.start(),m.end()))
indices.append(a)
print(indices)
This returns:
[[0, 2], [0, 2], [1, 3]]
Trying to get:
[[0, 2], [[0, 2], [1, 3]]]
so that it is clear that:
[[0, 2], [1, 3]]
are indices matches on 'cde' in the example above.
list((m.start(),m.end()))is normally spelled[m.start(), m.end()].