Skip to main content
 首页 » 编程设计

python之如何拆分列表内的多个字符串

2025年05月04日40daizhj

问题的背景是我想为电影列表创建一个搜索引擎。

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'