prog
prog

Reputation: 1073

extracting the actual output of generator in a list

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']

Upvotes: 0

Views: 393

Answers (2)

prog
prog

Reputation: 1073

This worked for me with the help of @PatrickArtner comment

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

Upvotes: 1

cyborg
cyborg

Reputation: 594

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)))

Upvotes: -1

Related Questions