extracting the actual output of generator in a list

310 Views Asked by At

I am trying to get the actual output from the generator but i get the ouput as generator object. Please help me achieve the actual output from the generator

import spacy
nlp = spacy.load('en')

def lemmatizer(words):
     yield from [w.lemma_ for w in nlp(words)]

list1 = ['birds hanging on street','people playing cards']

a = list(map(lemmatizer,list1))

Output:

a
[<generator object....>,
<generator object....>]

Expected output:

a
['birds hang on street',
'people play card']
2

There are 2 best solutions below

1
prog On BEST ANSWER

This worked for me with the help of @PatrickArtner comment

a = list(map(list, map(lemmatizer,list1)))
b = list(map(' '.join, a))
1
cyborg On

Use next to yield from the generator. Adding next like a = list(next(map(lemmatizer,list1))) should work.

import spacy
nlp = spacy.load('en')

def lemmatizer(words):
     yield from [w.lemma_ for w in nlp(words)]

list1 = ['birds hanging on street','people playing cards']

a = list(next(map(lemmatizer,list1)))