Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vectorized string processing in a NumPy array

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]
like image 952
tarrasch Avatar asked Apr 27 '26 23:04

tarrasch


1 Answers

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')
like image 125
Saullo G. P. Castro Avatar answered Apr 30 '26 13:04

Saullo G. P. Castro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!