【问题标题】:Why am I getting this Python Index error?为什么我会收到此 Python 索引错误?
【发布时间】:2015-08-18 22:58:45
【问题描述】:

我在使用我的代码时收到以下索引错误。此代码适用于用于股票技术分析的 Aroon 指标。错误消息说明如下。我正在使用 Python27。

Traceback(最近一次调用最后一次): 文件“C:\Python\Aroon.py”,第 46 行,在 阿隆(20) 文件“C:\Python\Aroon.py”,第 37 行,位于 aron 打印高[x] IndexError:索引 106 超出轴 0 的范围,大小为 106

示例数据位于http://sentdex.com/sampleData.txt 我将其复制到我自己的文本文件中。代码如下。它打印数据,但随后我收到以下错误消息,我正在尝试找出原因。

import numpy as np
import time

sampleData = open("sampleData.txt", "r").read()
splitData = sampleData.split("\n")

date, closep, highp, lowp, openp, volume = np.loadtxt(splitData,delimiter=",", unpack=True)


def aroon(tf):

    AroonUp = []
    AroonDown = []
    AroonDate = []

    x = tf

    while x <= len(date):
        Aroon_Up = ((highp[x-tf:x].tolist().index(max(highp[x-tf:x])))/float(tf))*100#numpy array to list.

        Aroon_Down = ((lowp[x-tf:x].tolist().index(min(lowp[x-tf:x])))/float(tf))*100#numpy array to list.

        AroonUp.append(Aroon_Up)
        AroonDown.append(Aroon_Down)
        AroonDate.append(date[x])

        x+=1

        print "######"
        print highp[x] # THIS IS LINE 37
        print Aroon_Up
        print "=="
        print lowp[x]
        print Aroon_Down
        print "#####"
    return AroonDate,AroonUp,AroonDown


aroon(20)

【问题讨论】:

  • 您应该在此处包含示例数据,而不是为此链接到外部资源。

标签: python numpy


【解决方案1】:

你应该改变这一行:

while x <= len(date):

到这里:

while x < len(date):

您的文件中有 106 行,它正在寻找第 107 行(从零开始)。

【讨论】:

  • 非常感谢,我现在可以运行它而不会出现错误消息。谢谢。感谢你们的回应。
【解决方案2】:

请记住,在 Python 中,索引从 0 而不是 1 开始。len(date) == 106,因此最大的有效索引是 105,而不是 106。尝试将 while 条件更改为

while x < len(date):

【讨论】:

    【解决方案3】:

    Python 开发人员竭尽全力确保您几乎不必手动编制索引,因为这很容易出错。

    解决问题的更 Pythonic 的方法是使用枚举:

    for x, date in enumerate(dates):
        if x < tf:
            continue
        # more code
    

    或者正如已经建议的那样,使用范围:

    for x in range(tf, len(dates)):
        # more code
    

    我个人会使用枚举。

    顺便说一句,我建议使用描述性变量名称,这样其他人(和您自己)更容易阅读代码。

    【讨论】:

      【解决方案4】:

      问题在于 Python 中如何指定范围:highp[x-tf:x]

      此列表中的最后一个索引是x-1,但您正在尝试打印索引x

      在列表中指定范围时,不包括最后一个元素。请参阅 Python shell 中的以下示例:

      >>> a=[0,1,2,3]
      >>> print a[0:1]
      [0]
      >>> print a[0:4]
      [0, 1, 2, 3]
      >>> print a[3:4]
      [3]
      >>> print a[0:5]
      [0, 1, 2, 3]
      >>> print a[0:6]
      [0, 1, 2, 3]
      >>> print a[0]
      0
      >>> print a[3]
      3
      >>> print a[4]
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      IndexError: list index out of range
      >>> 
      

      lowp[x-tf:x] 也会有同样的问题

      第二个问题是你先增加 x (x+=1) 然后在你的打印语句中使用 x 作为索引,你应该颠倒顺序:

      print "######"
      print highp[x] # THIS IS LINE 37
      print Aroon_Up
      print "=="
      print lowp[x]
      print Aroon_Down
      print "#####"
      
      x+=1
      

      【讨论】:

        【解决方案5】:

        你可以使用 for 循环。在 python shell 中查看以下示例

        对于 xrange(1,4) 中的 x: ... 打印 x ... 1 2 3

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-01-23
          • 1970-01-01
          • 1970-01-01
          • 2018-06-22
          • 2016-01-01
          • 2020-10-11
          相关资源
          最近更新 更多