【问题标题】:StopIteration Error when using next in Python 3 using nltk使用 nltk 在 Python 3 中使用 next 时出现 StopIteration 错误
【发布时间】:2021-07-23 23:49:13
【问题描述】:

我想在 Python 3 中运行代码。它在 Python 2.7 中运行,并且输入不为空。部分代码如下:

parser = nltk.ChartParser(zinc_grammar.GCFG)
parse_trees = [next(parser.parse(t)) for t in tokens]

但它在底线显示了 StopIteration 错误。有谁知道如何解决它?有什么替代品吗?

【问题讨论】:

  • 您显示的代码没有提供足够的问题信息,请提供可重现的示例stackoverflow.com/help/minimal-reproducible-example
  • 你能想出parser.parse(t) 可能产生空序列的原因吗?当您使用带有空序列的next 时,您认为应该发生什么?文档对此有何评论?当您尝试将python next stopiteration 放入搜索引擎时发生了什么?
  • 向我们展示引发此错误的 try...except 代码。

标签: python python-3.x nltk next stopiteration


【解决方案1】:

我意识到问题出在tokens 的定义上,因为我使用了lambda 函数。我改了,问题就解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-20
    • 1970-01-01
    • 2022-01-26
    • 2014-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多