[Numpy-discussion] String manipulation

David Goldsmith d_l_goldsmith@yahoo....
Tue Jul 21 12:16:52 CDT 2009


Hi, Chris.  Look at this, _I'm_ answering one of _your_ questions (correctly, I hope):

--- On Tue, 7/21/09, Christopher Barker <Chris.Barker@noaa.gov> wrote:

> I don't see why:
> 
> np.array('a string', dtype='S1')
> 
> results in a length (1,) array, for instance.
> 
> Actually, I think I do -- numpy is treating the string as a
> single 
> scalar, rather than a sequence of characters, and doing its
> best to 
> convert that scaler to a length one string. However, I

Well, as for why "[it's doing] its best to convert that scalar to a length one string," that's because you used dtype='S1' instead of dtype='S8':

>>> np.array('a string', dtype='S1')
array('a',
      dtype='|S1')
>>> np.array('a string', dtype='S8')
array('a string',
      dtype='|S8')

but as for shape, I can't reproduce your result at all:

>>> np.array('a string', dtype='S1').shape
()
>>> np.array('a string', dtype='S8').shape
()

and as for size, yes, in both cases it is treating the entire string as a single "element":

>>> np.array('a string', dtype='S1').size
1
>>> np.array('a string', dtype='S8').size
1

> don't know if 
> there is a compelling reason why it should do that -- in
> other contexts, 
> Python generally treats strings as a sequence of
> characters.

I see your point (global consistency) but personally, IMO, if you want that kind of string behavior, work in the Python namesapace: I like (though I've yet to actually use it) options (if you want to process a single string array element in the "standard" way, extract it from the array first w/ an index - it's only a few more characters of code).

:-)

DG

> -Chris
> 
> 
> 
> 
> 
> 
> 
> 
> 
> -- 
> Christopher Barker, Ph.D.
> Oceanographer
> 
> Emergency Response Division
> NOAA/NOS/OR&R           
> (206) 526-6959   voice
> 7600 Sand Point Way NE   (206)
> 526-6329   fax
> Seattle, WA  98115   
>    (206) 526-6317   main
> reception
> 
> Chris.Barker@noaa.gov
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
> 


      


More information about the NumPy-Discussion mailing list