7

I know can use string.find() to find a substring in a string.

But what is the easiest way to find out if one of the array items has a substring match in a string without using a loop?

Pseudocode:

string = 'I would like an apple.'
search = ['apple','orange', 'banana']
string.find(search) # == True

3 Answers 3

25

You could use a generator expression (which somehow is a loop)

any(x in string for x in search)

The generator expression is the part inside the parentheses. It creates an iterable that returns the value of x in string for each x in the tuple search. x in string in turn returns whether string contains the substring x. Finally, the Python built-in any() iterates over the iterable it gets passed and returns if any of its items evaluate to True.

Alternatively, you could use a regular expression to avoid the loop:

import re
re.search("|".join(search), string)

I would go for the first solution, since regular expressions have pitfalls (escaping etc.).

Sign up to request clarification or add additional context in comments.

6 Comments

+1 for generator expression. -1 for regex. +1 for recommending against regex. Huzzah! +1!
@nmichaels: I included the regex example only because of the "no loop" requirement.
@Sven: I figured. My tongue was pretty firmly in cheek there; this is a solid answer.
@Sven Marnach To cut the possible symbolic meaning of some characters in search , according to the rules of re, it can be done: re.search("|".join(re.escape(search)), string) What other pitfall then remain ?
@eyquem: In this particular example, escaping might be the only pitfall.
|
3

Strings in Python are sequences, and you can do a quick membership test by just asking if one string exists inside of another:

>>> mystr = "I'd like an apple"
>>> 'apple' in mystr
True

Sven got it right in his first answer above. To check if any of several strings exist in some other string, you'd do:

>>> ls = ['apple', 'orange']
>>> any(x in mystr for x in ls)
True

Worth noting for future reference is that the built-in 'all()' function would return true only if all items in 'ls' were members of 'mystr':

>>> ls = ['apple', 'orange']
>>> all(x in mystr for x in ls)
False
>>> ls = ['apple', 'like']
>>> all(x in mystr for x in ls)
True

Comments

1

The simpler is

import re
regx = re.compile('[ ,;:!?.:]')

string = 'I would like an apple.'
search = ['apple','orange', 'banana']

print any(x in regx.split(string) for x in search)

EDIT

Correction, after having read Sven's answer: evidently, string has to not be splited, stupid ! any(x in string for x in search) works pretty well

If you want no loop:

import re
regx = re.compile('[ ,;:!?.:]')

string = 'I would like an apple.'
search = ['apple','orange', 'banana']
print regx.split(string)

print set(regx.split(string)) & set(search)

result

set(['apple'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.