How to check if a string contains only characters from a given set in python

泄露秘密 提交于 2019-11-30 14:15:00

Here are some odd ;-) ways to do it:

good = set('1234567890^-+x')

if set(input_string) <= good:
    # it's good
else:
    # it's bad

or

if input_string.strip('1234567890^-+x'):
    # it's bad!
else:
    # it's good

Use a regular expression:

import re

if re.match('^[-0-9^+x]*$', text):
    # Valid input

The re module comes with Python 2.5, and is your fastest option.

Demo:

>>> re.match('^[-0-9^+x]*$', '1x2^4-2')
<_sre.SRE_Match object at 0x10f0b6780>
  1. You can convert the valid chars to a set, as sets offer faster lookup
  2. Then you can use all function like this

    valid_chars = set("1234567890^-+x")  # Converting to a set
    if all(char in valid_chars for char in input_string):
        # Do stuff if input is valid
    
  3. We can convert the input string also a set and check if all characters in the inputstring is in the valid list.

    valid_chars = set("1234567890^-+x")  # Converting to a set
    if set(input_string).issubset(valid_chars):
        # Do stuff if input is valid
    

What about just convert both the string into set and checking input_set is subset of good_set as below:

>>> good_set = set('1234567890^-+x')
>>> input_set1 = set('xajfb123')
>>> input_set2 = set('122-32+x')
>>> input_set1.issubset(good_set)
False
>>> input_set2.issubset(good_set)
True
>>>

Yet another way to do it, now using string.translate():

>>> import string
>>> all_chars = string.maketrans('', '')
>>> has_only = lambda s, valid_chars: not s.translate(all_chars, valid_chars)
>>> has_only("abc", "1234567890^-+x.")
False
>>> has_only("x^2", "1234567890^-+x.")
True

It is not the most readable way. It should be one of the fastest if you need it.

whitelist = '1234567890^-+x'

str = 'x^2+2x+1'
min([ch in whitelist for ch in str])
True


str='x**2 + 1' 
min([ch in whitelist for ch in str])
False
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!