0

I have a list of words:

list1 = ['foo', 'baz', 'bat']

And an array of strings:

list2 = ['foo word word word', 'word baz word word', 'word word word word', 'word word bat word']
string_arr = np.array(list2)

I want to enumerate through the array and return a boolean if the element in the array contains any of the values in the list. The output to be a list of booleans:

[True, True, False, True]

Right now all I have is this code which just gives me a list of indices, which I don't want:

idx_mask = [idx for idx, element in enumerate(string_arr) if any(x in element for x in list1)]

How can I just get a list of booleans?

6
  • Do you want the text to contain a substring or specifically a word? if list1 contains w, should result be True on all of them? Commented Mar 6, 2021 at 21:50
  • @MikeB No, only if it contains the full word, the answer below works Commented Mar 6, 2021 at 21:53
  • Making list2 an array does not gain you anything. It's faster to iterate through a list, and numpy does not implement any special, faster, string methods. Commented Mar 6, 2021 at 21:53
  • @5sWithCrackScreen - no it doesn't. It works only for this example. Add 'w' to the list1 and all answers will be True Commented Mar 6, 2021 at 22:09
  • @MikeB Yes, but I'm only looking for like words in my case. Basically the SQL equivalent of like '%foo% Commented Mar 6, 2021 at 22:13

2 Answers 2

2
print([any(x in element for x in list1) for element in list2])
Sign up to request clarification or add additional context in comments.

Comments

1

To find only full word matches, you should match them to each string from list2 split by space to make a word array:

print([any(x in element.split(' ') for x in list1) for element in list2]) 

Test:

list1 = ['foo', 'baz', 'bat', 'w']
list2 = ['foo word word word', 'word baz word word', 'word word word word', 'word word bat word']

results are:
[True, True, False, True]
which is the expected result.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.