检查是否有任何列表项在字符串中

Check if any list items are in a string

我创建了一个 class 来检查列表 self._name 中的任何项目是否在字符串 song1[=18 中=].我使用断言来检查它是否为真。如果我用 print 更改断言,我得到的结果是 None,这就是断言不起作用的原因。结果应该是真的,但我不确定我做错了什么。

class Song:
    def __init__(self, artist, title):
        self._artist = artist
        self._title = title
            
    def checkArtist(self, name):
        self._name = name.split() #split to list since just a first name or last name is allowed
        any(artistname in self._artist for artistname in self._name)

def main_program():
    song1 = Song("Lady Gaga and Bradley Cooper", "Shallow")

    #test, should return True for both asserts since the first is an exact match and the second contains "Gaga"
    assert(song1.checkArtist("Lady Gaga and Bradley Cooper"))
    assert(song1.checkArtist("Lord Gaga"))
main_program() 

当我将它隔离在 class 之外时它起作用了。我不太熟悉使用 classes,所以我可能对 class.

做错了什么
song2="Lady Gaga and Bradley Cooper"

name2="Lord Gaga"
name2=name2.split()

any(namecheck in song2 for namecheck in name2) #returns True
class Song:
    def __init__(self, artist, title):
        self._artist = artist
        self._title = title
            
    def checkArtist(self, name):
        return any(artistname in self._artist for artistname in name.split())

您需要return您的布尔值!