这是一个矢量化的方法 –
def slicer_vectorized(a,start,end):
b = a.view('S1').reshape(len(a),-1)[:,start:end]
return np.fromstring(b.tostring(),dtype='S'+str(end-start))
样品运行 –
In [68]: a = np.array(['hello', 'how', 'are', 'you'])
In [69]: slicer_vectorized(a,1,3)
Out[69]:
array(['el', 'ow', 're', 'ou'],
dtype='|S2')
In [70]: slicer_vectorized(a,0,3)
Out[70]:
array(['hel', 'how', 'are', 'you'],
dtype='|S3')
运行时测试 –
测试其他作者发布的所有方法,我可以在最后运行,还包括本文前面的向量化方法.
这是时间 –
In [53]: # Setup input array
...: a = np.array(['hello', 'how', 'are', 'you'])
...: a = np.repeat(a,10000)
...:
# @Alberto Garcia-Raboso's answer
In [54]: %timeit slicer(1, 3)(a)
10 loops, best of 3: 23.5 ms per loop
# @hapaulj's answer
In [55]: %timeit np.frompyfunc(lambda x:x[1:3],1,1)(a)
100 loops, best of 3: 11.6 ms per loop
# Using loop-comprehension
In [56]: %timeit np.array([i[1:3] for i in a])
100 loops, best of 3: 12.1 ms per loop
# From this post
In [57]: %timeit slicer_vectorized(a,1,3)
1000 loops, best of 3: 787 µs per loop