【问题标题】:extracting the actual output of generator in a list在列表中提取生成器的实际输出
【发布时间】:2021-12-28 10:43:45
【问题描述】:

我正在尝试从生成器获取实际输出,但我将输出作为生成器对象。请帮我实现生成器的实际输出

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

【问题讨论】:

  • a = list(map(list, map(lemmatizer,list1)))
  • @PatrickArtner 请写下答案,现在我将输出作为标记列表中的列表,请作为每个句子加入内部列表,谢谢..我应该为此更改一下词形还原函数吗?

标签: python generator spacy yield-from


【解决方案1】:

在@PatrickArtner 评论的帮助下,这对我有用

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

【讨论】:

  • 您可以在大约 28 小时左右自行接受 :) 很高兴它有所帮助
【解决方案2】:

使用next 从生成器中产生。像a = list(next(map(lemmatizer,list1))) 这样添加next 应该可以。

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

【讨论】:

  • next() 只从生成器中检索一件事?
猜你喜欢
  • 2021-09-22
  • 2023-03-24
  • 2020-06-01
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
相关资源
最近更新 更多