Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String comparison in Numpy

Tags:

python

numpy

In the following example

In [8]: import numpy as np

In [9]: strings = np.array(['hello    ', 'world    '], dtype='|S10')

In [10]: strings == 'hello'
Out[10]: array([False, False], dtype=bool)

The comparison fails because of the whitespace. Is there a Numpy built-in function that does the equivalent of

In [12]: np.array([x.strip()=='hello' for x in strings])
Out[12]: array([ True, False], dtype=bool)

which does give the correct result?

like image 775
astrofrog Avatar asked Mar 23 '10 15:03

astrofrog


People also ask

Can you use NumPy for strings?

The numpy. char module provides a set of vectorized string operations for arrays of type numpy.

How do you compare two string arrays in Python?

To perform element-wise comparison of two string arrays using a comparison operator, use the numpy. compare_chararrays() method in Python Numpy. The arr1 and arr2 are the two input string arrays of the same shape to be compared.

What is NumPy STR_?

numpy. string_ is the NumPy datatype used for arrays containing fixed-width byte strings. On the other hand, str is a native Python type and can not be used as a datatype for NumPy arrays*.

How do I compare elements in a NumPy array?

To compare each element of a NumPy array arr against the scalar x using any of the greater (>), greater equal (>=), smaller (<), smaller equal (<=), or equal (==) operators, use the broadcasting feature with the array as one operand and the scalar as another operand.


1 Answers

Numpy provides vectorised string operations for arrays similar to Python's string methods. They are in the numpy.char module.

http://docs.scipy.org/doc/numpy/reference/routines.char.html

import numpy as np

strings = np.array(['hello    ', 'world    '], dtype='|S10')

print np.char.strip(strings) == 'hello'
# prints [ True False]

Hope this is helpful.

like image 136
Gary Kerr Avatar answered Oct 11 '22 20:10

Gary Kerr