Movatterモバイル変換


[0]ホーム

URL:


Open In App

Testing if string contains an element fromlist is checking whether any of the individual items in a list appear within a given string.

Using any() with a generator expression

any()is the most efficient way to check if any element from the list is present in the list.

Python
s="Python is powerful and versatile."el=["powerful","versatile","fast"]# Check if any element in the list exists in the string# using any() and a generator expressionres=any(eleminsforeleminel)print(res)

Output
True

Explanation:

  • Theany() function evaluates if at least one element in the generator expression is True.
  • Thegenerator expression iterates through the list and checks each element’s presence in the string using the 'in' operator.
  • This approach is efficient as it short-circuits and stops processing once a match is found.

Let's explore some more methods to check how we can test if string contains elements from a list.

Using a for loop

This approach explicitly iterates through the list using afor loop to check for the presence of elements in the string.

Python
s="Python is powerful and versatile."el=["powerful","versatile","fast"]# Initialize the result variable to Falseres=False# Iterate through each element in the listforeleminel:ifelemins:res=Truebreakprint(res)

Output
True

Explanation:

  • Theloop iterates through each element in the list 'el' and checks if it exists in the string 's' using the 'in' operator.
  • If a match is found, the loop exits early using break, which saves unnecessary iterations.

Using set intersection

Using set intersection method is effective when both the string and the list of elements are relatively short.

Python
s="Python is powerful and versatile."el=["powerful","versatile","fast"]# Split the string into individual words using the split() method'res=bool(set(s.split())&set(el))print(res)

Output
True

Explanation:

  • Thesplit() method breaks the string into individual words and sets are created from the string and list elements.
  • The & operator computes the intersection of the two sets to check for common elements.

Using regular expressions

Regular expressionsprovide flexibility for more complex matching scenarios but are less efficient for simple tasks.

Python
importres="Python is powerful and versatile."el=["powerful","versatile","fast"]# Compile a regular expression pattern to search for any of the elements in the listpattern=re.compile('|'.join(map(re.escape,el)))res=bool(pattern.search(s))print(res)

Output
True

Explanation:

  • Thejoin()method creates a single pattern from the list of elements, separated by | (logical OR).
  • There.compile()function compiles the pattern for faster matching and search checks for its presence in the string.
  • This method is less efficient for simple substring checks due to overhead from compiling patterns.

Improve

Explore

Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp