【问题标题】:Getting: 'IndexError: index 1 is out of bounds for axis 0 with size 0' but can't tell why获取:'IndexError: index 1 is out of bounds for axis 0 with size 0' 但不知道为什么
【发布时间】:2020-08-26 21:58:14
【问题描述】:

尝试将值填充到我的数组时出现索引错误,但我不明白为什么会发生此错误。

    Hist = HistData[:pos, :]
    
    if Hist.shape[0] != 0:
        for y in range(2012, 2019):
            hist_pos = 0
            YearHist = np.zeros((150000, 1))
            for k in range(Hist.shape[0]):
                tm = time.gmtime(Hist[k, 0])
                if tm.tm_year == y:
                    YearHist[hist_pos, 0] = Hist[k, vartype]
                    hist_pos+=1
                
                YearHist = YearHist[:hist_pos]

这是违规代码的 sn-p。这被设置为之前运行良好的代码。对象“Hist”是一个 Nx11 二维数组。 N 是从较大数据结构中获取的非零条目的数量。有时它可能是零,但出于设置年度数组的目的,我忽略了这些。

当我运行代码时,我得到:

Traceback(最近一次调用最后一次):

文件“YearlyHistoPlots.py”,第 34 行,在

 YearHist[hist_pos, 0] = Hist[k, vartype]

IndexError:索引 1 超出轴 0 的范围,大小为 0

这让我很困惑。我明确地将 YearHist 定义为轴 0,大小为 150k,并且任何时候 Hist 轴为零,大小为零,这行代码不应该执行。我有什么遗漏吗?

【问题讨论】:

标签: python python-3.x numpy


【解决方案1】:

问题出在最后一行,你不小心把它放在了for-k-Loop中,但它属于for-y循环。删除一个Tab就可以了。

                YearHist = YearHist[:hist_pos]

变成

            YearHist = YearHist[:hist_pos]

【讨论】:

    猜你喜欢
    • 2017-04-09
    • 2021-12-17
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 2019-09-21
    • 2020-03-13
    • 2020-05-22
    • 1970-01-01
    相关资源
    最近更新 更多