Python 为什么获取“列表索引超出范围”错误?

Python 为什么获取“列表索引超出范围”错误?,python,list,indexoutofboundsexception,Python,List,Indexoutofboundsexception,我写了一个代码来下载列表中单词的同义词,locations。但是,由于一个单词可以有多种含义,我使用了另一个列表,含义,来指向我想要该单词含义的序列号。然后根据找到的这些同义词计算单词之间的相似性,然后将它们保存在文件中 from nltk.corpus import wordnet as wn from textblob import Word from textblob.wordnet import Synset locations = ['access', 'airport', 'ame

我写了一个代码来下载列表中单词的同义词,
locations
。但是,由于一个单词可以有多种含义,我使用了另一个列表,
含义
,来指向我想要该单词含义的序列号。然后根据找到的这些同义词计算单词之间的相似性,然后将它们保存在文件中

from nltk.corpus import wordnet as wn
from textblob import Word
from textblob.wordnet import Synset

locations = ['access', 'airport', 'amenity', 'area', 'atm', 'barrier', 'bay', 'bench', 'boundary', 'bridge', 'building', 'bus', 'cafe', 'car', 'coast', 'continue', 'created', 'defibrillator', 'drinking', 'embankment', 'entrance', 'ferry', 'foot', 'fountain', 'fuel', 'gate', 'golf', 'gps', 'grave', 'highway', 'horse', 'hospital', 'house', 'land', 'layer', 'leisure', 'man', 'market', 'marketplace', 'height', 'name', 'natural', 'exit', 'way', 'park', 'parking', 'place', 'worship', 'playground', 'police', 'station', 'post', 'mail', 'power', 'private', 'public', 'railway', 'ref', 'residential', 'restaurant', 'road', 'route', 'school', 'shelter', 'shop', 'source', 'sport', 'toilet', 'tourism', 'unknown', 'vehicle', 'vending', 'machine', 'village', 'wall', 'waste', 'waterway'];

meaning = [0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 5, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 11, 0, 1, 0, 0, 3, 0, 4, 0, 0, 3, 4, 0, 0, 0, 10, 0, 9, 1, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]

ncols = len(locations)
nrows = len(locations)
matrix = [[0] * ncols for i in range(nrows)]

for i in range(0,len(locations)):
    word1 = Word(locations[i])
    SS1 = word1.synsets[meaning[i]]
    for j in range(0,len(locations)):
        word2 = Word(locations[j])
        SS2 = word1.synsets[meaning[j]]
        matrix[i][j] = SS1.path_similarity(SS2)

f = open('Similarities.csv', 'w')
print(matrix, file=f)
但代码给出了以下错误:

SS2 = word1.synsets[meaning[j]]
IndexError: list index out of range
当我打印出
I
j
的值时,我发现它一直打印到I=0和j=36。这意味着当j=36时,会产生误差。索引36处的列表中的单词是
man
,索引36处的
含义的值是11

那么,为什么会发生此错误?我如何修复它

EDIT:错误在
SS2=word1.synsets[意思是[j]]
中。它应该是
SS2=word2.synsets[意思是[j]]
。抱歉。

len(word1.synsets)
返回8和
类型(word1.synsets)
返回列表。这是一个索引为0到7的列表

您的“含义”列表在索引36处包含11个。因此,当循环到达
word1.synsets[11]
时,您会得到索引超出范围的错误

正如Jose所说,7是“含义”中可以包含的最大整数。

len(word1.synsets)
返回8,而
type(word1.synsets)
返回列表。这是一个索引为0到7的列表

您的“含义”列表在索引36处包含11个。因此,当循环到达
word1.synsets[11]
时,您会得到索引超出范围的错误


正如Jose所说,7是“意义”中的最大整数。

打印(word1.synsets)
的输出是什么?似乎
word1.synsets
没有
位置中的项数多。
@tomwylie它打印出
人的13种不同意义。我现在已经在问题中包含了截图。@JoséSánchez
word1.synsets
包含13个东西,我想提取第11个。我现在在问题中加入了截图。为什么
SS2=word1.synsets[means[j]]
而不是
SS2=word2.synsets[means[j]]
。你没有在你的代码中真正使用
word2
print(word1.synsets)
的输出是什么?似乎
word1.synsets
没有
位置中的项数多
@tomwylie它打印出13种不同意义的
。我现在已经在问题中包含了截图。@JoséSánchez
word1.synsets
包含13个东西,我想提取第11个。我现在在问题中加入了截图。为什么
SS2=word1.synsets[means[j]]
而不是
SS2=word2.synsets[means[j]]
。你没有在你的代码中真正使用
word2
len(word1.synsets)
为我返回13(我编辑了问题以将屏幕截图包含在问题中)。
len(word1.synsets)
为我返回13(我编辑了问题以将屏幕截图包含在问题中)。