Test if string ONLY contains given characters

前端 未结 7 532
甜味超标
甜味超标 2021-01-12 13:19

What\'s the easiest way to check if a string only contains certain specified characters in Python? (Without using RegEx or anything, of course)

Specifically, I have

7条回答
  •  星月不相逢
    2021-01-12 14:01

    Below is the code:

    a = ['aba', 'acba', 'caz']
    needle = 'abc'
    
    def onlyNeedle(word):
        for letter in word:
            if letter not in needle:
                return False
    
        return True
    
    a = filter(onlyNeedle, a)
    
    print a
    

提交回复
热议问题