IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
13.1 Introduction


13.1 Introduction

numarray, like Numeric, has support for arrays of objects in addition to arrays of numbers. Arrays of objects are supported by the numarray.objects module. The ObjectArray class is used to represent object arrays.

The easiest way to construct an object array is to use the numarray.objects.array() function. For example:

  >>> import numarray.objects as obj
  >>> o = obj.array(['S', 'J', 1, 'M'])
  >>> print o
  ['S' 'J' 1 'M']
  >>> print o + o
  ['SS' 'JJ' 2 'MM']

In this example, the array contains 3 Python strings and an integer, but the array elements can be any Python object. For each pair of elements, the add operator is applied. For strings, add is defined as string concatenation. For integers, add is defined as numerical addition. For a class object, the __add__ and __radd__ methods would define the result.

ObjectArray is defined as a subclass of numarray's structural array class, NDArray. As a result, we can do the usual indexing and slicing:

  >>> import numarray.objects as obj
  >>> print s[0]
  'S'
  >>> print s[:2]
  ['S' 'J']
  >>> s[:2] = 'changed'
  >>> print s
  ['changed' 'changed' 1 'M']
  >>> a = obj.fromlist(numarray.arange(100), shape=(10,10))
  >>> a[2:5, 2:5]
  ObjectArray([[22, 23, 24],
               [32, 33, 34],
               [42, 43, 44]])

Send comments to the NumArray community.