Home > Software design >  Check All substring is present in a string in python
Check All substring is present in a string in python

Time:09-22

I want to get True if all X,Y,Z and A are in sentence , the below code works if any of them is there but i want "TRUE" if all are there else False

df['D'] = (df['CT'].str.contains("X|Y|Z|A", case = False))

CodePudding user response:

If Y always follows X in each string, then you change your regex to "X.*Y". Adding Z and A is left as an exercise for the reader.

CodePudding user response:

Might look a bit convoluted but I'd use Series.map with all:

import pandas as pd

TRUE_VALUES = ['x', 'y', 'z', 'a']

df = pd.DataFrame({'CT': ['xa', 'zf', 'c', 'XyZa']})
df['D'] = df['CT'].map(lambda val: all(true_val in val.lower()
                                       for true_val in TRUE_VALUES))
print(df)

outputs

     CT      D
0    xa  False
1    zf  False
2     c  False
3  XyZa   True

CodePudding user response:

Maybe something like this (?):

def check_all_in(s: str, chars: str, sep='|'):
    for c in chars.split(sep):
        if c not in s:
            return False
    return True

Test run:

teststring1 = 'A cow jumped over the moon.'
teststring2 = "A Yellow cow that I thought was a Zebra just ate my sister's Xylophone."

testchars = 'X|Y|Z|A'

print(check_all_in(teststring1, testchars))
print(check_all_in(teststring2, testchars))
  • Related