【问题标题】:Python numpy - IndexError: too many indices for arrayPython numpy - IndexError:数组索引过多
【发布时间】:2015-12-09 14:19:59
【问题描述】:

我对 python 还很陌生,但这个例子对我不起作用。我相信它是为了索引 x,但我不是 100% 确定。

import numpy as np

y = np.array([0,1,2,3,4,5,2,4,1,9])
x = np.array(['e','l','t','d','m','f','c','j','x','a'])

print
print
print "Y"
print type(y)
print y
print
print
print "X"
print type(x)
print x
print
print


result = x[0, y]

我得到的确切错误是

Traceback (most recent call last):
File "test.py", line 20, in <module>
result = x[0, y]
IndexError: too many indices for array

【问题讨论】:

  • 您的预期结果是什么? result 应该是什么样的?

标签: numpy indexing


【解决方案1】:

要从数组中获取值,您必须使用x[index]。您无法在括号中获取传递 2 个参数的值

【讨论】:

    【解决方案2】:

    x 不是多维数组。您必须将 0 附加到 y:

     result = x[0+y]
    

    【讨论】:

      【解决方案3】:

      我明白我现在做错了什么,x 应该是np.array([['e','l','t','d','m','f','c','j','x','a']]), 不是x = np.array(['e','l','t','d','m','f','c','j','x','a'])

      感谢您的回复!

      【讨论】:

        猜你喜欢
        • 2018-05-23
        • 1970-01-01
        • 2019-09-04
        • 1970-01-01
        • 2020-04-10
        • 2020-03-04
        • 1970-01-01
        • 2020-09-26
        • 1970-01-01
        相关资源
        最近更新 更多