在列表中提取生成器的实际输出
extracting the actual output of generator in a list
我正在尝试从生成器获取实际输出,但我得到的输出是生成器对象。请帮助我实现生成器的实际输出
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))
输出:
a
[<generator object....>,
<generator object....>]
预期输出:
a
['birds hang on street',
'people play card']
使用next
从生成器中产生。添加 next
像 a = list(next(map(lemmatizer,list1)))
应该有效。
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)))
这在@PatrickArtner 评论的帮助下对我有用
a = list(map(list, map(lemmatizer,list1)))
b = list(map(' '.join, a))
我正在尝试从生成器获取实际输出,但我得到的输出是生成器对象。请帮助我实现生成器的实际输出
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))
输出:
a
[<generator object....>,
<generator object....>]
预期输出:
a
['birds hang on street',
'people play card']
使用next
从生成器中产生。添加 next
像 a = list(next(map(lemmatizer,list1)))
应该有效。
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)))
这在@PatrickArtner 评论的帮助下对我有用
a = list(map(list, map(lemmatizer,list1)))
b = list(map(' '.join, a))