当我在 numpy 中创建一维数组并使用字符串(包含数字)对其进行索引时,我得到了预期的错误:
>>> import numpy as np
>>> a = np.arange(15)
>>> a['10']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: field named 10 not found.
但是,当我创建一个二维数组并使用两个字符串进行索引时,它没有给出错误并返回元素,就好像字符串首先转换为整数一样
>>> b = np.arange(15).reshape(3,5)
>>> b
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
>>> b[1, 2]
7
>>> b['1', '2']
7
这是怎么回事?为什么我在二维情况下不会出错?