if substring in string, when substring has multiple values

  • Last Update :
  • Techknowledgy :
1._
substring1 or substring2 or substring3

You want:

substring1 in string or substring2 in string or substring3 in string

Or:

substrings = (substring1, substring2, substring3)
if any(s in string
   for s in substrings):
1._
>>> string = 'hello' >>>
   s1 = 'he' >>>
   s2 = 'll' >>>
   s3 = 'o' >>>
   (s1 or s2 or s3)
'he' >>>
'he' in string
True

The problem here is that you are first evaluating (s1 or s2 or s3) which will give you s1 if s1 does not evaluate to false. In a boolean context, empty strings evaluate to false:

>>> s1 = '' >>>
   (s1 or s2 or s3)
'll'

Its because python uses lazy evaluation... for example...

def error():
   raise ArithmeticError

print "nothing"
or error()

Will Print "nothing" because it evaluates to True in python code does not check error, which would ordinarily raise an error and stop the script from continuing...

print False or error() #raises error and breaks script(substring1 or substring2 or substring3) #returns the first true value

The correct code should be

for i in list:
   if (substring1 in i) or(substring2 in i) or(substring3 in i):
      print(i + " found!")

The above code can find substring when they have multiple values.

(substring1 or substring2 or substring3)

This expression will return the first item in brackets that is not null. For example, if subtring1 is not null, it will return the value of substring1. When substring1 is not null,

(substring1 or substring2 or substring3)

You probably want any:

if any(substr in string
      for substr in substrs):
   # At least one match

Suggestion : 2

My main data source is quite large, but essentially looks like the below: ,Similar problems have been answered before but the solutions are not quite working in my case. I'm relatively new to DAX, so not sure if this is easily solvable.,Still take some time but not nearly as long as typing everything down. You can do a lot of copy/paste/dragging to duplicate values in Excel.,Yes! This would be the way to go for long term solution, especially if that list changes. All you'd need to do is update that list somewhere (e.g. Excel in SharePoint...), bring that list into PQ, and reference it in a PQ formula similar to the IN operator in DAX.

Please use the following DAX formula to create a calculated column:

Column = IF(
   SUMX(MatchList,
      FIND(
         UPPER(MatchList[Keyword]),
         UPPER(Companies[Company]), , 0
      )
   ) > 0, “YES!”, “Probably Not”
)

Please use the following DAX formula to create a calculated column:

Column = IF(
   SUMX(MatchList,
      FIND(
         UPPER(MatchList[Keyword]),
         UPPER(Companies[Company]), , 0
      )
   ) > 0, “YES!”, “Probably Not”
)

Suggestion : 3

Using regular expressions, we can easily check multiple substrings in a single-line statement.,We use the findall() method of the re module to get all the matches as a list of strings and pass it to any() method to get the result in True or False.,This is one of the fastest method to check whether a string contains any of the given multiple substrings in Python.,We append the boolean results to a list and pass it to any() function to return True or False indicating whether any of the sub-strings are present in the string.

The any() function in Python accepts an iterable (such as List, Dictionary, Tuple, etc) as a parameter and returns True if any of the elements in the iterable is True.

substrings = ['python', 'python3', 'programming']
string = 'Learn programming at pencilprogrammer.com'

result_list = []
for x in substrings:
   # append True / False
for substring x
result_list.append(x in string)

#call any() with boolean results list
print(any(result_list))

We can overcome this by changing the strings to the same case (e.g lower), inside of the loop body:

substrings = ['mY', 'naME', 'is', 'KuMAR']
string = 'Author name: Adarsh Kumar'

result_list = []
for x in substrings:
   # append True / False
for substring x
result_list.append(x.lower() in string.lower())

#call any() with boolean results list
print(any(result_list))

We use the findall() method of the re module to get all the matches as a list of strings and pass it to any() method to get the result in True or False.

import re

string = 'Python is good for Machine Learning and Data-Science'

""
"
pass substrings separated by | as 1 st argument
and main string value as 2n d argument.
Additionally, we can pass re.IGNORECASE paramter as
3 rd argument to make matching
case -insensitive.
""
"
match_list = re.findall(r 'python|machine|good', string, re.IGNORECASE)

print(any(match_list))

Suggestion : 4

Last Updated : 17 Aug, 2022

Present at index 5

5

Suggestion : 5

Remote Work 2022 , Remote Work 2022

Syntax of in:
substring in string
Code to check if python string contains a substring:
if "Hire" in "Hire the top freelancers":
print("Exists")
else:
   print("Does not exist")

#Output - Exists

This method converts the string to lower case. As strings are immutable, this would not affect the original string.

if "hire" in "Hire the top freelancers".lower():
   print("Exists")
else:
   print("Does not exist")

#Output - Exists
Code using index():
try:
"Hire the top freelancers".index("Hire")
except ValueError:
   print("Does not exist")
else:
   print(sting.index(sti))

#Output = 0

index() is case sensitive, ensure you use the .lower() function to avoid bugs.

try:
"Hire the top freelancers".lower().index("hire")
except ValueError:
   print("Does not exist")
else:
   print(sting.index(sti))

#Output = 0