How can i process text in numpy arrays elegantly?
I can always iterate over the array, but is there some magic oneliner also possible? I am just learning python and want to do it in a way that looks good also.
example of what i want:
for y in data['filename']:
first = 12
last = y[1][12:].find('.')
y= y[1][first+1:last+12]
You can use a numpy.char.array(), for example:
from string import find
import numpy as np
a = np.char.array(['cmd.py', 'matrix.txt', 'print.txt', 'test.txt', 'testpickle.test', 'Thumbs.db', 'tmp.py', 'tmp.txt', 'tmp2.py'])
find(a, '.py')
#array([ 3, -1, -1, -1, -1, -1, 3, -1, 4])
np.char.array(a.split('.'))[:,0]
#chararray(['cmd', 'matrix', 'print', 'test', 'testpickle', 'Thumbs', 'tmp', 'tmp', 'tmp2'], dtype='|S10')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With