[Numpy-discussion] Empty strings not empty?

Charles R Harris charlesr.harris@gmail....
Wed Dec 30 12:34:31 CST 2009


On Tue, Dec 29, 2009 at 4:35 PM, Matthew Brett <matthew.brett@gmail.com>wrote:

> Hi,
>
> I was surprised by this - should I have been?
>
> In [35]: e = np.array(['a'])
>
> In [36]: e.shape
> Out[36]: (1,)
>
> In [37]: e.size
> Out[37]: 1
>
> In [38]: e.tostring()
> Out[38]: 'a'
>
> In [39]: f = np.array(['a'])
>
> In [40]: f.shape == e.shape
> Out[40]: True
>
> In [41]: f.size == e.size
> Out[41]: True
>
> In [42]: f.tostring()
> Out[42]: 'a'
>
> In [43]: z = np.array(['\x00'])
>
> In [44]: z.shape
> Out[44]: (1,)
>
> In [45]: z.size
> Out[45]: 1
>
> In [46]: z
> Out[46]:
> array([''],
>      dtype='|S1')
>
> That is, an empty string array seems to be the same as a string array
> with a single 0 byte, including having shape (1,) and size 1...
>
>
It isn't empty:

In [3]: array(['\x00']).dtype
Out[3]: dtype('|S1')

In [4]: array(['\x00']).tostring()
Out[4]: '\x00'

In [5]: array(['\x00'])[0]
Out[5]: ''

Looks like a printing problem to me, something in __repr__ for the string
array. It seems that trailing zeros are trimmed off.

In [11]: array(['a\x00\x00'])
Out[11]:
array(['a'],
      dtype='|S3')

In [12]: array(['a\x00b'])
Out[12]:
array(['a\x00b'],
      dtype='|S3')

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20091230/964e12d8/attachment.html 


More information about the NumPy-Discussion mailing list