Access line by line to a numpy structured array

℡╲_俬逩灬. 提交于 2019-12-12 03:44:52

问题


I am trying to access to a structured array line by line by iterating on the values of one field of it but even if the value iterate well, the slice of the array doesn't change. Here is my 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]

gives me :

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

At each iteration, I always have the same slice of the array... strange ?


回答1:


The last line is not right. The array index evaluates to True or False rather than doing a lookup of a named column. Try this:

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


来源:https://stackoverflow.com/questions/36694549/access-line-by-line-to-a-numpy-structured-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!