Issue
If this is the nparray i have
arr_str = np.array(['10', '100', '301', '23423'])
I want to convert into like this using slicing get the last char in the str a[-1] where a is element in nparray
arr_slice = np.array(['0', '0', '1', '3'])
just like how we can add numbers to each element of nparray like
arr_str.asType(int) + 5
Solution
Why not use a list comprehension?
>>> np.array([x[-1] for x in arr_str])
array(['0', '0', '1', '3'], dtype='<U1')
Alternatively you can use the %
modulo operator:
>>> arr_str.astype(int) % 10
array([0, 0, 1, 3])
Answered By - Ivan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.