[Numpy-discussion] add.reduce for character arrays

Todd Miller jmiller at stsci.edu
Mon Apr 19 07:06:07 EDT 2004

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.

> Thanks.
>                                                      Regards, Faheem.
> **********************************************************************
> In [43]: s
> Out[43]:
> CharArray([['a', 'b', 'c'],
>            ['d', 'e', 'f'],
>            ['g', 'h', 'i']])
> In [44]: numarray.add.reduce(s,1)
> ---------------------------------------------------------------------------
> error                                     Traceback (most recent call
> last)
> /home/faheem/wc/corrmodel/boost/<console>
> error: getShape: sequence object nested more than MAXDIM
> -------------------------------------------------------
> This SF.Net email is sponsored by: IBM Linux Tutorials
> Free Linux tutorial presented by Daniel Robbins, President and CEO of
> GenToo technologies. Learn everything from fundamentals to system
> administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion
Todd Miller <jmiller at stsci.edu>

More information about the NumPy-Discussion mailing list