Python索引超出范围?
问题描述:
我使用阿拉伯文字词来查找同义词;它工作正常使用下面的代码,并将其输出正确的同义词:但是我要遍历上面的一部分,我改字(YXZ)每次Python索引超出范围?
import unicodedata
from nltk.corpus import wordnet as wn
yxz='work'
jan = wn.synsets(yxz)[0]
abc=jan.lemma_names(lang='arb')
for bca in abc: #Converting from unicode to arabic done
nfkd_form = unicodedata.normalize('NFKD', bca)
encoded=nfkd_form.encode('utf-8')#this works fine
encoded= u"".join([c for c in nfkd_form if not unicodedata.combining(c)])
print encoded
,但它不工作,因为指数是出范围:(我有一个XML文档我想要得到的动词只在某些句子中的同义词,动词是标签<v>
之间存在的XML文档中)
Synonyms=[]
for phrase in root.findall('./PHRASE'):
ens = {en.get('x'): en.text for en in phrase.findall('en')}
if 'ORG' in ens and 'PERS' in ens:
if (((ens["ORG"] ==u"جامعة بيت لحم")and (ens["PERS"]==u" ه أحمد")) or ((ens["ORG"] ==u"جامعة كولومبيا.")and (ens["PERS"]==u"رئيس الجمهورية السيد محمد المنصف المرزوقي")) or ((ens["ORG"] ==u"معمل باريكادي ")and (ens["PERS"]==u"رئيس فنزويلا")) or ((ens["ORG"] ==u"شركة جوجل")and (ens["PERS"]==u"لاري بيدج وسيرغي برين")) or((ens["ORG"] ==u"محترفه الباريسي")and (ens["PERS"]==u"بول"))):
for v in phrase.findall('V'):
#----------------------------------------ENGLISH SYNONYM TRIAL---------------------------
print("------ English Synonym Trial----------")
#-------Step 8.3] Google Translate API working fine) now want it to translate from ar to en from Diacritics----------
#-----8.3.1] Translate Diactrics Array words to english-----------------------
gs = goslate.Goslate()
engVerb=gs.translate(unicode(v.text), 'en') #english word is the output
print("---EngVerb---")
print(engVerb)
#-----8.3.2] use Arabic Wordnet to get the synonyms[English->output unicode] Working -----------------------
#yxz='work'
jan = wn.synsets(engVerb)[0]
abc=jan.lemma_names(lang='arb')
for bca in abc: #Converting from unicode to arabic done
nfkd_form = unicodedata.normalize('NFKD', bca)
encoded=nfkd_form.encode('utf-8')#this works fine
encoded= u"".join([c for c in nfkd_form if not unicodedata.combining(c)])
#print encoded
Synonyms.append(encoded)
print("----------------------------PRINTING SYNONYMS---------------------------")
print Synonyms
不过,我总是得到错误
jan = wn2.synsets(engVerb)[0]
IndexError:列表索引超出范围
答
该错误意味着wn2.synsets(engVerb)
是一个空列表(使用print
调试,它帮助了很多),而你试图访问一个不存在的第一个元素。
试试这个:
x = wn2.synsets(engVerb)
if len(x) == 0:
continue
else:
jan = x[0]