Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to reverse strings in a NumPy array?

I want to reverse the order of characters in each string element of a NumPy array. For example, given the following input:

array(['2', '3', '5', '7', '11', '13', '17', '19', '23', '29', '31', '37',
       '41', '43', '47', '53', '59', '61', '67', '71', '73', '79', '83',
       '89', '97'], dtype='<U2')

I want to obtain the following output (without using Python for loop):

array(['2', '3', '5', '7', '11', '31', '71', '91', '32', '92', '13', '73',
       '14', '34', '74', '35', '95', '16', '76', '17', '37', '97', '38',
       '98', '79'], dtype='<U2')

I know that I can use arr[::-1] to reverse the order of elements in a NumPy array, but that isn’t the topic of this question, and np.array([e[::-1] for e in arr]) is inefficient and against the point of NumPy.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The array was created using a vectorized version of the base conversion function np.vectorize(to_base_str).

How can I reverse the order of characters in each string element of a NumPy array using vectorization? I have searched online but have not found a solution. Note that arr[..., ::-1] does not work for string elements in a NumPy array.

(Code is mine, but I did use the "AI suggested edits" feature)

>Solution :

np.array([e[::-1] for e in arr]) is the straight forward way of doing this, and is NOT bad numpy. or bypass numpy entirely with [e[::-1] for e in arr.tolist()]. You could also do something similar with np.vectorize or np.frompyfunc. These might scale a bit better.

‘vectorize’ in numpy means using compiled methods (and operators) to do the necessary iterations in compiled code. Those are nearly all numeric operations. For strings, numpy uses Python string methods. It does not have its own compiled string operations. Even the np.char functions use python string methods.

So there’s no numpy equivalent to astr[::-1].

Some comparative times

In [16]: timeit np.array([s[::-1] for s in arr])
36.1 µs ± 151 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [17]: timeit np.array([s[::-1] for s in arr.tolist()])
21.1 µs ± 76.5 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [18]: timeit [s[::-1] for s in arr.tolist()]
8.29 µs ± 23.2 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)

In [20]: timeit np.vectorize(lambda s: s[::-1])(arr)
65.9 µs ± 165 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)

In [21]: timeit np.frompyfunc(lambda s: s[::-1],1,1)(arr)
20.3 µs ± 76.5 ns per loop (mean ± std. dev. of 7 runs, 10,000 loops each)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading