Py学习  »  Python

在python中将字符串中的字符与列表匹配

georgebills123 • 5 年前 • 1598 次点击  

前任:

>>>match(['hello', 'world'], "hold"])
>>>True

到目前为止我得到的是:

def match(myList , myString):
    for i in myList:
        "".join(str(i) for i in myList)
    for e in myString:
        if any(e == i):
            return True

但是我得到一个类型错误:“bool”对象不可iterable

编辑: 我让它这么做:

def match(myList, myString):
   listString = "".join(myList)
    return all(character in listString for character in myString)
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/54830
 
1598 次点击  
文章 [ 1 ]  |  最新文章 5 年前
tdelaney
Reply   •   1 楼
tdelaney    5 年前

not set("".join(myList)).isdisjoint(set(myString)) 会这样做,但考虑到您可能正在处理for循环,并且您的代码很接近,请执行以下操作

def match(myList , myString):
    myChars = "".join(myList)
    for c in myString:
        if c in myChars:
            return True
    return False