vendredi 6 mars 2020

Looping over a list to find a match for another list

Let's say have a list like this:

[[t[0] for t in tagger.tag(s)] for s in corpus.sents()[:5]]

Looks like this:

[['1'], ['Hermann', 'Hesse', 'Der', 'Steppenwolf'], ['Erzählung'], ['Vorwort', 'des', 'Herausgebers'],
['Dieses', 'Buch', 'enthält', 'die', 'uns', 'geblichenen', 'Aufzeichnungen', 'jenes', 'Mannes', ',',
'welchen', 'wir', 'mit', 'einem', 'Ausdruck', ',', 'den', 'er', 'selbst', 'mehrmals', 'gebrauchte', ',',
'den', '«', 'Steppenwolf', '»', 'nannten', '.']]

Now, I need so select all lists within the list of lists that contain any of the strings listed here:

vds_nennen = ['nennen', 'nenne', 'nennst', 'nennt', 'nenne', 'nennest', 'nannte', 
'nanntest', 'nannten', 'nanntet', 'nennte', 'nenntest', 'nennten', 'nenntet', 'genannt'] 

So the desired output is:

[['Dieses', 'Buch', 'enthält', 'die', 'uns', 'geblichenen', 'Aufzeichnungen', 
'jenes', 'Mannes', ',', 'welchen', 'wir', 'mit', 'einem', 'Ausdruck', ',', 'den',
'er', 'selbst', 'mehrmals', 'gebrauchte', ',', 'den', '«', 'Steppenwolf', '»', 
'nannten', '.']]

(because the last list in the list of lists contains 'nannten' that vds_nennen list also has)

I tried to loop over the list vds_nennen this way:

[[t[0] for t in tagger.tag(s)] for s in corpus.sents()[:5] if [vds for vds in vds_nennen] in s]

But got nothing:

[]

Sorry in advance for my ignorance in looping... Any suggestion will be appreciated.

Aucun commentaire:

Enregistrer un commentaire