简体   繁体   English

NumPy数组中的矢量化字符串处理

[英]Vectorized string processing in a NumPy array

How can i process text in numpy arrays elegantly? 我如何优雅地处理numpy数组中的文本?

I can always iterate over the array, but is there some magic oneliner also possible? 我总是可以迭代数组,但是还有一些神奇的oneliner吗? I am just learning python and want to do it in a way that looks good also. 我只是在学习python,并希望以一种看起来也不错的方式来做。

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: 您可以使用numpy.char.array() ,例如:

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM