I have a dictionary mydict which contains some filenames as keys and text within them as values.
I am extracting a list of words from the text in each file. Words are stored in a list mywords.
I have tried the following.
mydict = {'File1': 'some text. \n Foo extract this. \n Bar extract this',
'File2': 'more text. \n Bar extract this too.'}
mywords = ['Foo', 'Bar']
mylist= []
for k,v in mydict.items():
for word in mywords:
extracted = (re.findall('^ ' + word + ".*", v, flags=re.IGNORECASE|re.MULTILINE))
mylist.append(extracted[:1])
This gives me
[[' Foo extract this. '],
[' Bar extract this'],
[],
[' Bar extract this too.']]
However, I want the output to have 2 nested lists (for each file) instead of a separate list each time it searches a word in a file.
Desired output:
[[' Foo extract this. '], [' Bar extract this']],
[[], [' Bar extract this too.']]