简体   繁体   中英

How to find all occurences of a substring in a numpy string array

I'm trying to find all occurences of a substring in a numpy string array. Let's say:

myArray = np.array(['Time', 'utc_sec', 'UTC_day', 'Utc_Hour'])
sub = 'utc'

It should be case insensitive, so the method should return [1,2,3].

A vectorized approach using np.char.lower and np.char.find

import numpy as np
myArray = np.array(['Time', 'utc_sec', 'UTC_day', 'Utc_Hour'])
res = np.where(np.char.find(np.char.lower(myArray), 'utc') > -1)[0]
print(res)

Output

[1 2 3]

The idea is to use np.char.lower to make np.char.find case-insensitive , then fetch the indices that contains the sub-string using np.where .

You can use if sub in string to check it.

import numpy as np

myArray = np.array(['Time', 'utc_sec', 'UTC_day', 'Utc_Hour'])
sub = 'utc'

count = 0
found = []
for item in myArray:
    if sub in item.lower():
        count += 1
        found.append(count)

print(found)

output:

[1, 2, 3]

We can use list comprehension te get the right indexes:

occ = [i for i in range(len(myArray)) if 'utc' in myArray[i].lower()]

Output

>>> print(occ)
... [1, 2, 3]

Let's make a general use from this question: we will set up a function returning occurences indexes of any sub-character inside a numpy string array .

get_occ_idx(sub, np_array):
    """ Occurences index of substring in a numpy string array
    """
    
    assert sub.islower(), f"Your substring '{sub}' must be lower case (should be : {sub.lower()})"
    assert all(isinstance(x, str)==False for x in np_array), "All items in the array must be strings"
    assert all(sub in x.lower() for x in np_array), f"There is no occurence of substring :'{sub}'"
    
    occ = [i for i in range(len(np_array)) if sub in np_array[i].lower()]
    
    return occ

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM