[Numpy-discussion] Empty strings not empty?

Warren Weckesser warren.weckesser@enthought....
Tue Dec 29 17:45:56 CST 2009


Hmmm... I don't see where you created  "an empty string array" in your 
examples.  All three of your arrays contain one element, so they are not 
empty.  The element in the array z happens to be zero, is all.

Here's an example of creating an empty string array:

In [2]: e = np.array([],dtype='S')

In [3]: e
Out[3]:
array([],
      dtype='|S1')

In [4]: e.shape
Out[4]: (0,)


Warren



Matthew Brett 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...
>
> Best,
>
> Matthew
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>   



More information about the NumPy-Discussion mailing list