Python 如何检查字符串中是否有字符(a-b)

Python 如何检查字符串中是否有字符(a-b),python,Python,我试图检查某个字符串是否包含来自a-z的任何字符。 我看到我可以在中使用,但这似乎不是像这样在弦上传递的最舒适的方式: if a in string if b in string if c in string 你能帮我找到正在做的函数/算法吗?它也适用于数字吗?尝试使用regex by import re If re.search(r"[a-z]", s): ... 将输入字符串转换为列表,然后按如下方式进行后期处理: list(set([x for x in a if x in b

我试图检查某个字符串是否包含来自a-z的任何字符。 我看到我可以在中使用,但这似乎不是像这样在弦上传递的最舒适的方式:

if a in string
if b in string
if c in string
你能帮我找到正在做的函数/算法吗?它也适用于数字吗?

尝试使用regex by

import re
If re.search(r"[a-z]", s):
    ...

将输入字符串转换为列表,然后按如下方式进行后期处理:

list(set([x for x in a if x in b]))
脚本:

STRING = ";alkd779-n;l--xswdlfkj"
TEST = "abcde"

string = list(STRING)
test = list(TEST)
matches =  list(set([x for x in string if x in test]))
contains_match = True if len(matches)>0 else False

print 'string         : %s' % string
print 'test           : %s' % test
print 'matches        : %s' % matches 
print 'contains match : %s' % contains_match

>>>
string         : [';', 'a', 'l', 'k', 'd', '7', '7', '9', '-', 'n', ';', 'l', '-', '-', 'x', 's', 'w', 'd', 'l', 'f', 'k', 'j']
test           : ['a', 'b', 'c', 'd', 'e']
matches        : ['a', 'd']
contains match : True

您研究过python中的re模块吗?您可以构造正则表达式,然后在字符串中搜索模式。借用重复中的,try anyc.islower for c in str.我想在不使用for循环的情况下使用它,我想谢谢你。那么为什么要在这个算法中使用in'set'函数呢?set会删除重复项,以便在测试列表中存在项的情况下,逐项从字符串列表移动到匹配列表