[英]Check if any list items are in a string
I have created a class that checks if any item of the list self._name is in the string song1 .我创建了一个类,如果列表self._name的任何产品在字符串松1支票。 I use assert to check if it is True.我使用断言来检查它是否为真。 If I change assert with print, I get that the result is None, which is why I the assert does not work.如果我用打印更改断言,我会得到结果为无,这就是我断言不起作用的原因。 It should come out as True, but I am unsure what I am doing wrong.它应该显示为 True,但我不确定我做错了什么。
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()
It works when I isolate it outside the class.当我将它隔离在课堂之外时,它就起作用了。 I am not too familiar with using classes, so I might have done something wrong with the 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())
You need to return your boolean value!你需要返回你的布尔值!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.