Home > Software engineering >  How to check for the presence of a substring in a list of strings
How to check for the presence of a substring in a list of strings

Time:02-15

I have a list of strings and I need to generate a boolean array indicating the presence of a substring in each cell of the list. How could this be implemented and generate an array rather than just return the strings from the list containing the substring?

For example, the check for:

my_array = np.array(['hello', 'this', 'is', 'a', 'testello'])

Should return:

check = np.array([1, 0, 0, 0, 1])

Or:

check = np.array([true, false, false, false, true])

When it checks for the substring 'ello'.

Thanks!

CodePudding user response:

Just make sure 'ello' is included in the element of 'my_array'. Below is the code to check if an element contains 'ello'.

import numpy as np

my_array = np.array(['hello', 'this', 'is', 'a', 'testello'])

check = list()
for data in my_array:
    if 'ello' in data:
        check.append(1)
    else:
        check.append(0)
check = np.array(check)

You can change 1 to True and 0 to False.

CodePudding user response:

You have a numpy array, not a list.

Anyway, considering a list (this would also work on a numpy array):

my_lst = ['hello', 'this', 'is', 'a', 'testello']

query = 'ello'
out = [query in e for e in my_lst]

# [True, False, False, False, True]

for a numpy array:

my_array = np.array(['hello', 'this', 'is', 'a', 'testello'])

out = np.core.defchararray.find(my_array, query)>0
# array([ True, False, False, False,  True])
  • Related