Вопрос

I can create a recarray and access its members by name:

import numpy as np

n = 20
x = np.recarray((n,), dtype=[('x',int),('y',float),('label',object)])
x.x[:] = range(n)
x.y[:] = np.arange(n)*0.08
x.label[:] =  ['%d bottles of beer on the wall' % i for i in range(n)]

and I can access the data by row index, or iterate over the rows:

>>> print x[3]
(3, 0.24, '3 bottles of beer on the wall')

But how can I iterate over the fields or get the kth field in the recarray?

Это было полезно?

Решение

recarrays have a field() method for this purpose. Using your example ...

>>> x.field(2)
... array(['0 bottles of beer on the wall', '1 bottles of beer on the wall',
       '2 bottles of beer on the wall', '3 bottles of beer on the wall',
       '4 bottles of beer on the wall', '5 bottles of beer on the wall',
       '6 bottles of beer on the wall', '7 bottles of beer on the wall',
       '8 bottles of beer on the wall', '9 bottles of beer on the wall',
       '10 bottles of beer on the wall', '11 bottles of beer on the wall',
       '12 bottles of beer on the wall', '13 bottles of beer on the wall',
       '14 bottles of beer on the wall', '15 bottles of beer on the wall',
       '16 bottles of beer on the wall', '17 bottles of beer on the wall',
       '18 bottles of beer on the wall', '19 bottles of beer on the wall'], dtype=object)
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top