在for-if循环中列出索引超出范围错误

时间:2018-04-17 15:03:23

标签: for-loop if-statement indexing error-handling

我有一个名为' df'的数据框。有一栏名为“#"'看起来像这样:

enter image description here

我有代码检查df.word中的i是否为单词,如果是,那么' True'被添加到列表中' boo'。虽然代码有效,但由于某种原因,它不断抛出错误消息:错误:发生以下错误:在处理真实结果后列出索引超出范围:

df=pd.read_csv(r"C:\Users\hiner\for_pydict.csv")
boo = list()
for i in range df.word:
    meaning=dictionary.meaning(i)
    if meaning is not None:
        d = 'True'
    else:
        d = 'False'
    boo.append(d)  

立即出局是:

Error: The Following Error occured: list index out of range
Error: The Following Error occured: list index out of range
Error: The Following Error occured: list index out of range

但是' boo'有我想要的结果:

boo
['True', 'False', 'True', 'False', 'True']

错误是指这一行:     含义= dictionary.meaning(I) 但我不知道我能做些什么来解决它。我试过带走变量,但没有骰子。为什么这个错误会抛出?作为解决方法,如何解决,如何防止打印/显示错误消息。我是否需要更改我的代码,或者是否需要应用异常处理?如果是,我该怎么做?

感谢。

0 个答案:

没有答案
相关问题