问题的背景是我想为电影列表创建一个搜索引擎。
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
不会工作,但我不确定还能做什么
最佳答案
试试这个方法:
flag=0
search = str(input("Search: "))
for movie in movies_list:
if search in movie:
flag=1
print(movie)
if not flag:
print("no match")
Python 方式:
movies_list=["Avatar", "Planet of the Apes", "Rise of the Apes", "Avatar the Second"]
def match_movie(name):
return [movie for movie in movies_list if name in movie] or 'No Match'
关于python - 如何拆分列表内的多个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36837192/