首页 > 解决方案 > 拥有包含多年电影的大量 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 的标题,以便用户输入更容易匹配标题(否则如果没有他们在括号中输入年份,它将显示为不存在)。

我遇到的问题是,一旦我只提取标题,我现在就有一个列表,我无法将其作为键添加到字典中。我也分别提取了电影年份,以及我想作为键值添加的那些年份。

我有很多“不可散列类型”列表错误,并查看了许多关于类似情况的帖子。我的情况似乎与我无关的是,我的标题对于某些标题以及许多行项目有空格和多个单词。

请告知我如何以另一种方式解决此问题;谢谢和欢呼~

标签: regexlistdictionary

解决方案


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",
}

推荐阅读