Reputation: 1705
I have an ndarray that looks like this:
In [1]: a
Out [1]: array(['x','y'], dtype=object)
Now I wanted to append a "z" to the end of it:
In [2]: print([a,'z'])
[array(['x','y'],dtype=object), 'z']
Instead, what I want is:
['x','y','z']
Any idea?
Upvotes: 3
Views: 24011
Reputation: 13465
As alternative to append (since you can use it for several iterables; check for example: PEP3132) you can use the "unpacking" symbol to do it:
import numpy as np
a = np.array(['x','y'], dtype=object)
b = np.array([*a, "z"])
print(*a, "z")
print(b)
The result is this:
x y z
['x' 'y' 'z']
Upvotes: 0
Reputation: 444
You can do it using numpy.append:
import numpy as np
a = np.array(['x','y'])
b = np.append(a,['z'])
In [8]:b
Out[8]: array(['x', 'y', 'z'], dtype='<U1')
Upvotes: 11
Reputation: 214957
You can use numpy.append
:
import numpy as np
a = np.array(['x', 'y'])
np.append(a, 'z')
# array(['x', 'y', 'z'],
# dtype='<U1')
Upvotes: 1