问题的背景是我想为电影列表创建一个搜索引擎。你知道吗
movies_list=["Avatar", "Planet of the Apes", "Rise of the Apes", "Avatar the Second"]
因此,我希望用户能够搜索,例如,Apes
,程序将显示
Planet of the Apes
Rise of the Apes
我想尝试但我知道行不通的代码是
movieSearch = movies_list.split()
search = input(str("Search: ")
for movie in movies_list
if(movieSearch == search):
print(movie)
if(movieSearch != search):
print("No Match")
主要是因为我知道movieSearch
行不通,但我只是不知道还能做什么
试试这个:
请这样做:
脓道:
您可以简单地使用以下内容:
请注意,这将只搜索准确的单词,并且区分大小写。例如,如果
search = "apes"
或search = "APES"
,那么上面的代码只会生成一个空列表。你知道吗要使其成为不区分大小写的搜索,可以使用^{} (或^{} )将字符串转换为其中一个大小写,然后进行比较。你知道吗
编辑:
i.split()
将给出准确的单词搜索结果。如果您想要部分搜索,那么只需使用i
。你知道吗相关问题 更多 >
编程相关推荐