为什么会出现“列表索引超出范围”错误?

问题描述 投票:0回答:1

我试图通过NLTK实现wordnet模块。但是我一直收到错误list index out of range。我的代码有什么问题?请帮我。

我的代码:

from nltk.corpus import wordnet

s=wordnet.synsets('worse')
for i in range(len(s)):
    print(s[i].lemmas()[i].name())

错误:

IndexError: list index out of range
python wordnet
1个回答
0
投票

s[i].lemmas()返回的列表中包含的项目少于原始s列表。要解决此问题,请直接遍历列表:

from nltk.corpus import wordnet

s = wordnet.synsets('worse')
for w in s:
    for l in w.lemmas():
        print(l.name())
© www.soinside.com 2019 - 2024. All rights reserved.