"LazyCorpusLoader"对象不可迭代 [英] 'LazyCorpusLoader' object is not iterable

查看:548
本文介绍了"LazyCorpusLoader"对象不可迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下示例创建一个字谜字典. 但是,它抛出TypeError: 'LazyCorpusLoader' object is not an iterator:

The following example creates an anagram dictionary. However, it throws a TypeError: 'LazyCorpusLoader' object is not an iterator:

import nltk
from nltk.corpus import words

anagrams = nltk.defaultdict(list)
for word in words:
    key = ''.join(sorted(word))
    anagrams[key].append(word)

print(anagrams['aeilnrt'])

推荐答案

您必须在words语料库对象上使用.words()方法.

You have to use the .words() method on the words corpus object.

具体来说:更改

for word in words:

for word in words.words():

它应该可以工作.

这篇关于"LazyCorpusLoader"对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆