python - Slice 1D Array in Numpy without loop -
i have array x
shown below:
x=np.array(["83838374747412e61e4c202c004d004d004d020202c3cf", "8383835f6260127314a0127c078e07090705023846c59f", "83838384817e14231d700fac09bc096808881e1c1bc68f", "8484835c535212600f860a1612b90fcf0fcf012a2ac6bf", "848484787a7a1a961bac1e731086005d005d025408c6cf", "8484845050620c300d500a9313e613e613012a2a5cc4bf", "838383757c7cf18f02192653070d03180318080101be6f", "8584845557570f090e830f4309e5080108012a2a2ac6df", "85858453536b07d608b3124c102a102a1026010101c61f", "83838384848411a926791c162048204820484d4444c3bf"], dtype=object)
these concatenated hex values need piece in order convert integers , apply conversion factors. want array such as:
[83,83,83,84,84,84,83,85,85,83]
which equivalent of x[:,0:2]
cannot piece in (10,)
array. trying similar character array in matlab. doing on millions of rows why trying avoid loop.
any help appreciated, thanks.
if you're after first 2 characters each hex value, 1 alternative recast array dtype
of '|s2'
:
>>> x.astype('|s2') array(['83', '83', '83', '84', '84', '84', '83', '85', '85', '83'], dtype='|s2')
this thought can generalised homecoming first n
characters each string.
arbitrary slicing of string arrays much more hard in numpy. answers on this stack overflow page explain why isn't best tool strings show can possible.
alternatively, pandas library facilitates fast vectorized operations (being built on top of numpy). has number of useful string operations makes slicing whole lot simpler plain numpy:
>>> import pandas pd >>> s = pd.series(x) >>> s.str.slice(2, 9) 0 8383747 1 83835f6 2 8383848 3 84835c5 4 8484787 5 8484505 6 8383757 7 8484555 8 8584535 9 8383848 dtype: object
python arrays numpy slice
No comments:
Post a Comment