[Numpy-discussion] np.zeros(2, 'S') returns empty strings.
Sat Jan 14 18:21:55 CST 2012
On Sat, Jan 14, 2012 at 5:25 PM, Benjamin Root <firstname.lastname@example.org> wrote:
> On Sat, Jan 14, 2012 at 4:16 PM, Benjamin Root <email@example.com> wrote:
>> On Sat, Jan 14, 2012 at 4:12 PM, Charles R Harris
>> <firstname.lastname@example.org> wrote:
>>> This sort of makes sense, but is it the 'correct' behavior?
>>> In : zeros(2, 'S')
>>> array(['', ''],
>>> It might be more consistent to return '0' instead, as in
>>> In : zeros(2, int).astype('S')
>>> array(['0', '0'],
I would be surprised if zeros is not an empty string, since an empty
string is the "zero" for string addition.
multiplication for strings doesn't exist, so ones can be anything even
>>> a = np.zeros(5,'S4')
>>> a[:] = 'b'
>>> reduce(lambda x,y: x+y, a)
>>> a = np.zeros(1,'S100')
>>> for i in range(5): a[:] = a.item() + 'a'
just as a logical argument, I have no idea what's practical since last
time I tried to use numpy strings, I didn't find string addition and
went back to double and triple list comprehension.
>> Whatever it should be, numpy is currently inconsistent:
>> >>> np.empty(2, 'S')
>> array(['0', '\xd4'],
>> >>> np.zeros(2, 'S')
>> array(['', ''],
>> >>> np.ones(2, 'S')
>> array(['1', '1'],
>> I would expect '0''s for the call to zeros() and empty strings for the
>> call to empty().
>> Ben Root
> On the other hand, it is fairly standard to assume that the values in the
> array returned by empty() to be random, uninitialized junk. So, maybe
> empty()'s current behavior is ok, but certainly zeros()'s and ones()'s
> behaviors need to be looked at.
> Ben Root
> NumPy-Discussion mailing list
More information about the NumPy-Discussion