Check if an array of strings contains a substring of another string

Question:

Let’s say I have an array of strings, along with some other sentences ie.


fruits = ['apple', 'orange', 'grape']

example1 = 'John really enjoys melons'
example2 = 'John would like an apple'

I want to return True if any of the values inside ‘fruits’ are a valid substring of text1 or text2.

So in this case text1 is going to be false, and text2 is going to be true. Obviously I can iterate over every value in fruits and check if it is present in the example text one by one for each example.

However, my actual data has hundreds of ‘examples’ I want to check, and my ‘fruits’ array is much longer. Is there a short/concise way to do this in Python? ie. Some sort of function that can be called instead of having to iterate over each value in "fruits" for every example.

Asked By: Akshay Chacko

||

Answers:

You could use any() with a generator expression to iterate over all the fruits: (It short-circuits, so once it finds one that’s True, it stops)

any(fruit in example1 for fruit in fruits) # True
any(fruit in example2 for fruit in fruits) # False
Answered By: pigrammer

You can try set intersection if you just want code in an easily readable fashion. Internally, it has to go through every word in each of your example string but using a set might optimize it a little.

fruits = ['apple', 'orange', 'grape']
setFruits = set(fruits)

exampleList = ['John really enjoys melons', 'John really enjoys apple']

for example in exampleList:
    setExample = set(example.split(' '))
    if len(fruits.intersection(setExample)) > 0:
        print('True')
    else:
        print('False')

This might be a little fast because your example strings can contain multiple copies of the same word and you don’t have to check them again and again.

Answered By: whiplash
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.