trshmanx
trshmanx

Reputation: 680

Some problems with numpy array

li = np.array(list("123"))
li[0] = "fff"
print(li)

prints ['f' '2' '3'] and not ['fff' '2' '3']. Why so? How can I tune/fix it?

Upvotes: 0

Views: 167

Answers (1)

L.Grozinger
L.Grozinger

Reputation: 2398

Why so?

Some debugging will help here. Take the first line for example:

>>> li = np.array(list("123"))

Then what is li?

>>> li
array(['1', '2', '3'], dtype='<U1') 

Note the datatype of li, <U1, the docs explain that this datatype is for a unicode string of length 1. numpy has chosen this type for you, based on the elements you provided in the constructor, in order that it can allocate memory for li and probably do some optimisation in the background.

What happens when you do li[0] = "fff" then, since "fff" is not a unicode string of length 1? Then, numpy will convert "fff" to a unicode string of length 1, which in this case, appears to be "f".

>>> li[0] = "fff"
>>> li
array(['f', '2', '3'], dtype='<U1')

Now the print output should make sense.

What to do about it

You can explicitly pass a datatype when you construct your array. E.g.

>>> li = np.array(list("123"), dtype="<U3")
>>> li
array(['1', '2', '3'], dtype='<U3')
>>> li[0] = "fff"
>>> li
array(['fff', '2', '3'], dtype='<U3')

You will of course then have to decide in advance which type the array really will be, in this case, the maximum length of the string you can have is 3.

If you can make no guarantees about the types in the array, consider instead using a list instead of a numpy.array as a data structure.

Upvotes: 2

Related Questions