Skip to content
Advertisement

How to create a mask of substrings using np.where or list comprehensions?

I have two python lists of strings that I would like to compare. The first is my main list, containing a series of long codes. The second is a list of partial strings.

input:
list1 = ['fda3232', 'fcg3224', 'kgj5543', '3323fda9832', 'ffz3392', '221gks9483', 'mnx8385', 'aaz9323', '332kgj4323'] 

list2 = ['fda', 'kgj', 'mxx', 'mnx']

The desired result is a mask of list 1, populated by the substrings from list two. If no match is found, list3 can return 0, np.nan, ‘-‘, or similar. In other words, I’m looking for the following:

output: 

list3 = ['fda', np.nan, 'kgj', 'fda', np.nan, np.nan, 'mnx', np.nan, 'kgj']

With help from folks in another thread, I was able to get close. However, these results return the values in list1, but I would like my result to return the matching substring from list2.

solution 1: 
list3 = [x if any(y in x for y in list2) else np.nan for x in list1]

solution 2:
list3 = np.where([np.sum(np.char.find(x, sub=list2)+1) for x in list1], list1, np.NaN) 

Advertisement

Answer

You may use:

import numpy as np

list1 = ['fda3232', 'fcg3224', 'kgj5543', '3323fda9832', 'ffz3392', '221gks9483', 'mnx8385', 'aaz9323', '332kgj4323']

list2 = ['fda', 'kgj', 'mxx', 'mnx']

def isin(haystack):
    for needle in list2:
        if needle in haystack:
            return needle
    return np.nan

list3 = [isin(haystack) for haystack in list1]
print(list3)

Which yields

['fda', nan, 'kgj', 'fda', nan, nan, 'mnx', nan, 'kgj']

Your could even put it in a comprehension:

list3 = [result[0]
         for haystack in list1
         for result in [[needle for needle in list2 if needle in haystack] or [np.nan]]]
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement