Test if string ONLY contains given characters

前端 未结 7 574
甜味超标
甜味超标 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 13:55

    I will assume your reluctance for regexp is not really an issue :

    strings = ['aba', 'acba', 'caz']
    given = "abc"
    filter(lambda value: re.match("^[" + given + "]$", value), strings)
    

提交回复
热议问题