Test if string ONLY contains given characters

前端 未结 7 535
甜味超标
甜味超标 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:54

    You can make use of sets:

    >>> l = ['aba', 'acba', 'caz']
    >>> s = set('abc')
    >>> [item for item in l if not set(item).difference(s)]
    ['aba', 'acba']
    

提交回复
热议问题