[Numpy-discussion] add.reduce for character arrays
faheem at email.unc.edu
Mon Apr 19 20:21:25 CDT 2004
On Mon, 19 Apr 2004, Todd Miller wrote:
> On Sat, 2004-04-17 at 19:06, Faheem Mitha wrote:
> > Hi,
> > The following does not work. I am trying to do something similar to
> > add.reduce, except that I want strings concatenated across the
> > appropriate axes. I was hoping that Python would "magically" recognise
> > that addition in strings translated to concatenation.
> Use object arrays (numarray.objects) instead. While CharArrays do have
> an overloaded add operator they don't support reduction. Object arrays
> have more general ufuncs which apply object oriented operators and also
> support reduction. In general, character arrays (numarray.strings) do
> not support the numerical ufuncs found in numarray
> > Incidentally, the error message is not terribly imformative.
> True enough. The numerical getShape code should recognize strings and
> raise an exception pointing out that strings can't be passed into
> numerical code. This is fixed in CVS.
> > Is there any way to make this work, or any other method people can
> > suggest, or do I need to write my own function?
> Again, use numarray.objects.
Yes, that works. Thanks.
In : numarray.objects.add.reduce(s,dim=1)
Out: ObjectArray(['abc', 'def', 'ghi'])
BTW, that makes me wonder what the rules are for the order in which this
reduction proceeds for arrays bigger than dimension two. Since there is no
total order in this case, the answer is not obvious. For string
concatentation (since it is not commutative) at least this matters. I
didn't see this documented in the manual but I may have missed it.
More information about the Numpy-discussion