【问题标题】:List index out of range when iterating in a loop - Python在循环中迭代时列表索引超出范围 - Python
【发布时间】:2018-11-27 22:47:42
【问题描述】:

我正在研究留一法模型验证过程。当我通过一个循环运行时,将列表中的一项进行测试,它会在 i = 19 时停止。但是当我使用 i = 19 手动逐个运行时,它运行良好。特征长度为 36。

for i in range(len(features)):
        # i = 18
        w_count = word_count[i]
        x_test_c = features[i][['count']].copy()
        x_test = features[i]
        x_test.drop('count', axis=1, inplace=True)
        x_train_list = features
        x_train_list.pop(i)
        y_test = summaries[i]
        y_train_list = summaries
        y_train_list.pop(i)

        x_train = merge_data(x_train_list)
        x_train.drop('count', axis=1, inplace=True)
        y_train = merge_data(y_train_list)
        print(x_train.shape,"\t",y_train.shape)
        print(x_test.shape,"\t",y_test.shape)

        model = sm.OLS(y_train, x_train, missing='drop').fit()

        predictions = model.predict(x_test)
        predictions = predictions.sort_values(ascending=False)

        print("\n\nLeave one out cross validation \nTest report:",i+1)
        match(predictions, w_count, x_test_c, y_test)

示例输出是这样的。

(sysenv) D:\pythonprojects\rec_proj>python main.py 
Leave one out cross validation
Test report: 1
total word count of report: 509
summary word count: ~ 127.25
['2.4', '1.5', '3.2']
Precision= 1.0
Recall= 0.21428571428571427
F1= 0.35294117647058826
....
Leave one out cross validation
Test report: 18
total word count of report: 380
summary word count: ~ 95.0
['5.3', '12.2', '1.14', '5.2']
Precision= 0.75
Recall= 0.12
F1= 0.20689655172413793

它在此迭代后停止。 错误是这样的。

Traceback (most recent call last):
  File "main.py", line 49, in <module>
    lou(df_len, df_summary, word_count)
  File "D:\pythonprojects\rec_proj\model_eval.py", line 33, in lou
    x_test_c = features[i][['count']].copy()
IndexError: list index out of range

但如果我插入 i = 18

Leave one out cross validation
Test report: 19
total word count of report: 741
summary word count: ~ 185.25
['3.10', '10.1', '2.2', '4.1', '5.3', '2.4']
Precision= 0.8333333333333334
Recall= 0.22727272727272727
F1= 0.35714285714285715

所以发现循环在 18、27、30、33、35 处失败。我无法调试它,因为手动插入这些值时它可以正常工作。

【问题讨论】:

    标签: python-3.x indexoutofboundsexception


    【解决方案1】:

    在 Python 中,range(n) 将产生从 0n 的所有数字。为了帮助可视化您的问题,假设我们有这个简单的程序:

    array = [0, 1, 2, 3, 4]
    for m in range(len(array)): # len(array) evaluates to 5
        print(m) 
    for n in array:
        print(n)
    

    输出(用空格替换换行符)将是:

    0 1 2 3 4 5
    0 1 2 3 4
    

    如您所见,range(len(array)) 超出了数组的长度。这是您的代码的来源。在第一行中,您启动了一个 for 循环,该循环将遍历 range(len(features)),但随后在第四行中您访问 features[i]。因此,在循环的最后一次迭代中,range 超出了数组的长度,Python 会抛出一个错误,因为代码试图访问一个不存在的 features 元素。

    【讨论】:

    • 我明白你的意思。但是如果你阅读了整篇文章,你可以看到它在 i = 17 处停止。特征长度为 36。由于它在 17 处失败,我更改了 range(17, len(features)) 等等。这就是我认为其他 i 发生相同错误的方式。
    • 另外,根据this,您对第一个循环的回答似乎不正确。我正在使用python 3.6。我刚刚测试了我的循环是否从 0 变为 36。不,它在 35 处停止。
    • 嗯。我一定一直在考虑范围函数的不同之处。我会把它拿下来。
    猜你喜欢
    • 2016-10-03
    • 1970-01-01
    • 2020-07-23
    • 2022-09-23
    • 2014-01-16
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多