如何检查python中的字符串是否包含来自另一个字符串的随机字符集

我有一个作为字符串的源词,我有一个字符串,人们在其中输入了一个词列表,但我如何检查输入的字符串是否只包含源词中的字符,但顺序不限

def check(input_string):
    import re
    #http://docs.python.org/library/re.html
    #re.search returns None if no position in the string matches the pattern
    #pattern to search for any character other then . a-z 0-9
    pattern =word
    if re.search(pattern,test_str):
        #Character other then . a-z 0-9 was found
        print('Invalid : %r' % (input_string,))
    else:
        #No character other then . a-z 0-9 was found
        print('Valid   : %r' % (input_string,))```
a68434576 回答:如何检查python中的字符串是否包含来自另一个字符串的随机字符集

使用支持检查子集的set

template = set(word)
if set(input_string) < template:
    print("OK")

如果你坚持使用正则表达式,把模板变成字符类:

template = re.compile(f'[{word}]+')
if template.fullmatch(input_string):
    print("OK")
,

定义乐趣():

s = "Test" # Word
b = "TEST" #input_string
a = True
for c in b:
    if c.lower() not in s.lower():
        a = False
        break
if (a == False):
    print("Character is not in s")
else:
    print("No Other characters found")
本文链接:https://www.f2er.com/1798.html

大家都在问