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

标签 python

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

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/

相关文章:

python - 学习 Pyramid (python) 并且正在与 @view_config 装饰器作斗争。它应该开箱即用吗?

python嵌套函数内存泄漏?

python - 我可以删除迁移目录中的 django 迁移文件吗

python - python 范围内的条件和

python - 使用变量来描述数组/列表索引的索引范围/切片

python - 如何从 QTableWidget 列获取行数据?

Python高效的字符串混淆

python - 在定义的像素位置上绘制均匀间隔的垂直线

python - "AttributeError: ' float ' object has no attribute ' 替换 '"安装 python 包时出错

python - 具有字段名称值的 Django ManyToMany 字段