regex - 拥有包含多年电影的大量 DVD 列表,无法将两者作为列表分开为字典格式
问题描述
您好,我正在使用 Python(3.8.2) for 循环,其中包含一个家庭成员给我的 DVD 列表,全部组织为:
A Walk Among the Tombstones (2014)
Blithe Spirit (2020)
Jeepers Creepers (2001)
The Place Beyond the Pines (2012)
ETC....
搜索代码:
inp = input().lower().title()
if inp in movieTitle:
print(inp+ " DVD is AVAILABLE")
else:
print("THIS DVD IS NOT AVAILABLE")
提取标题/年份的代码:
for lin in L:
movieName = re.findall('(^.*)\s', lin)
movieTitle.append(movieName)
movieYr = re.findall('(\(....\))$', lin)
movieYear.append(movieYr)
注意:('L' 是从 .txt 文件的字符串创建的原始列表)
我正在通过用户输入创建搜索以检查现有标题。我已经成功地使用 re.findall 检索了 DVD 的标题,以便用户输入更容易匹配标题(否则如果没有他们在括号中输入年份,它将显示为不存在)。
我遇到的问题是,一旦我只提取标题,我现在就有一个列表,我无法将其作为键添加到字典中。我也分别提取了电影年份,以及我想作为键值添加的那些年份。
我有很多“不可散列类型”列表错误,并查看了许多关于类似情况的帖子。我的情况似乎与我无关的是,我的标题对于某些标题以及许多行项目有空格和多个单词。
请告知我如何以另一种方式解决此问题;谢谢和欢呼~
解决方案
将re.findall
返回 alist
因此您不能将其用作字典键。您可以使用re.search
并获取正确的搜索组。例如:
import re
with open("your_file.txt", "r") as f_in:
L = f_in.readlines()
movies = {}
for lin in L:
movieName = re.search(r"^([^(]+)", lin).group(1).strip()
movieYr = re.search(r"\((\d+)\)$", lin).group(1)
movies[movieName] = movieYr
inp = input("Search for movie: ").lower().title()
if inp in movies:
print(inp + " DVD is AVAILABLE")
else:
print("THIS DVD IS NOT AVAILABLE")
印刷:
Search for movie: Blithe Spirit
Blithe Spirit DVD is AVAILABLE
movies
字典是:
{
"A Walk Among the Tombstones": "2014",
"Blithe Spirit": "2020",
"Jeepers Creepers": "2001",
"The Place Beyond the Pines": "2012",
}
推荐阅读
- sublimetext3 - Sublime Text 3 - 当我左右移动鼠标时,编辑器窗口左右滚动
- reactjs - 运行测试时未实现 window.alert
- java - JTextPane 在突出显示和滚动时具有重叠文本
- swift - Swift / Firebase:有没有办法在应用程序后台检查 Firebase 数据库中的更新?
- bash - 需要帮助在 bash 脚本中使用 grep/egrep 匹配事物
- css - 可以有多个 ":root" 选择器吗?
- python - 列表列表中的距离
- python - 使用带有 shuffle 参数的 tf.data.Dataset.list_files() 加载的 tfrecord 时出现意外结果
- python-3.x - 如何将多个列表合并为一个列表?
- python - 使用 Python 将基于 pdf 的网页下载为 pdf