简体   繁体   中英

Dictionary of numpy array in python

I want to create a data structure of empty numpy array something of this sort:

d[1].foo = numpy.arange(x)
d[1].bar = numpy.arange(x)
d[2].foo = numpy.arange(x)
d[2].bar = numpy.arange(x)

What would be the best option ... a list of dictionaries containing numpy arrays?

If I define a simple class like:

class MyObj(object):
    pass
     .

I could create a dictionary with several of these objects:

In [819]: d={1:MyObj(), 2:MyObj()}

and then assign attributes to each object

In [820]: d[1].foo=np.arange(3)
In [821]: d[1].bar=np.arange(3)
In [822]: d[2].foo=np.arange(3)
In [823]: d[2].bar=np.arange(3)
In [824]: d
Out[824]: {1: <__main__.MyObj at 0xaf20cfac>, 2: <__main__.MyObj at 0xaf20c4cc>}

Since I didn't define a repr or str the print display isn't very interesting;

In [825]: vars(d[2])
Out[825]: {'bar': array([0, 1, 2]), 'foo': array([0, 1, 2])}

I could also made a list with these objects

In [826]: dl = [None, d[1], d[2]]
In [827]: dl
Out[827]: [None, <__main__.MyObj at 0xaf20cfac>, <__main__.MyObj at 0xaf20c4cc>]
In [828]: vars(dl[1])
Out[828]: {'bar': array([0, 1, 2]), 'foo': array([0, 1, 2])}

So both a list and dictionary can be indexed (so can an array); but the .foo syntax is used to access object attributes.

===============

An entirely different way of creating a structure with this kind of access is to use a recarray - this is a numpy array subclass that allows you to access dtype fields with attribute names

In [829]: R=np.recarray((3,), dtype=[('foo','O'),('bar','O')])
In [830]: R
Out[830]: 
rec.array([(None, None), (None, None), (None, None)], 
          dtype=[('foo', 'O'), ('bar', 'O')])
In [831]: R[1].foo=np.arange(3)
In [832]: R[2].bar=np.arange(4)
In [833]: R
Out[833]: 
rec.array([(None, None), (array([0, 1, 2]), None), (None, array([0, 1, 2, 3]))], 
          dtype=[('foo', 'O'), ('bar', 'O')])

Here I defined the fields as taking object dtype, which allows me to assign anything, including other arrays to each attribute. But usually the dtype is something more specific like int, float, string.

I can view the foo attribute/field for all items in the array R :

In [834]: R.foo
Out[834]: array([None, array([0, 1, 2]), None], dtype=object)
In [835]: R['bar']
Out[835]: array([None, None, array([0, 1, 2, 3])], dtype=object)

A recarray has a special method that allows access to the fields via attribute syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM