逐行访问 numpy 结构化数组

Access line by line to a numpy structured array

我试图通过迭代其中一个字段的值来逐行访问结构化数组,但即使值迭代良好,数组的切片也不会改变。这是我的 SWE :

import numpy as np
dt=np.dtype([('name',np.unicode,80),('x',np.float),('y',np.float)])
a=np.array( [('a',0.,0.),('b',0.,0.),('c',0.,0.) ],dtype=dt)
for n in a['name']:
  print n,a['name'==n]

给我 :

a (u'a', 0.0, 0.0)
b (u'a', 0.0, 0.0)
c (u'a', 0.0, 0.0)

在每次迭代中,我总是有相同的数组切片...奇怪吗?

最后一行不对。数组索引的计算结果为 True 或 False,而不是查找命名列。 试试这个:

for n in a['name']:
    print n,a[a['name']==n]