[Numpy-discussion] Using dtype=object
Stéfan van der Walt
stefan at sun.ac.za
Tue Nov 15 19:46:46 EST 2011
On Mon, Nov 14, 2011 at 9:08 PM, Nathan Faggian <nathan.faggian at gmail.com>wrote:
> I am interested in the use of numpy with native python objects, like so:
>
> In [91]: import collections
> In [92]: testContainer = collections.namedtuple('testContainer', 'att1
> att2 att3')
> In [93]: test1 = testContainer(1, 2, 3)
> In [94]: test2 = testContainer(4, 5, 6)
> In [95]: test1
> Out[95]: testContainer(att1=1, att2=2, att3=3)
> In [96]: test2
> Out[96]: testContainer(att1=4, att2=5, att3=6)
> In [97]: x = np.empty((2,2), dtype=object)
> In [98]: x[0,0] = test1
> In [99]: x[1,1] = test2
> In [100]: x
> Out[100]:
> array([[testContainer(att1=1, att2=2, att3=3), None],
> [None, testContainer(att1=4, att2=5, att3=6)]], dtype=object)
>
> Does anyone know if it possible to form a mask using the attributes of
> the objects stored in the ndarray?
>
Maybe something like this:
def attr_equal(attribute, value):
def _func(x):
if x is None:
return False
else:
return getattr(x, attribute) == value
return np.vectorize(_func)
print attr_equal('att2', 5)(x)
print attr_equal('att1', 1)(x)
Regards
Stéfan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20111115/1f5ef927/attachment.html>
More information about the NumPy-Discussion
mailing list