Hello I am using a Python(3.8.2) for loop with this list of DVD's a family member has given me, all organized as:
A Walk Among the Tombstones (2014)
Blithe Spirit (2020)
Jeepers Creepers (2001)
The Place Beyond the Pines (2012)
etc....
Search code:
inp = input().lower().title()
if inp in movieTitle:
print(inp " DVD is AVAILABLE")
else:
print("THIS DVD IS NOT AVAILABLE")
Code to extract titles/years:
for lin in L:
movieName = re.findall('(^.*)\s', lin)
movieTitle.append(movieName)
movieYr = re.findall('(\(....\))$', lin)
movieYear.append(movieYr)
note: ('L' is the original list created from the string of the .txt file)
I am creating a search via user input to check for existing titles. I have been successful at retrieving just the titles of the DVD's using re.findall so that the user input easier matches the title (otherwise it will show as non existant without them entering the year in parentheses).
The ISSUE I am having is that once I extract just the titles, I now have a list in which I cannot add into a dictionary as a Key. The movie years I have extracted separately as well, and those I want to add as the Value to the key.
I have had many "unhashable type" list errors and have reviewed many posts regarding a similar situation. Where my situation doesn't seem to relate, is that my titles have spaces and multiple words for certain titles as well as many line items.
Please advise how I may approach this in another way; thanks and cheers~
CodePudding user response:
The re.findall
will return a list
so you cannot use that as a dictionary key. You can use re.search
and the get correct search group. For example:
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")
Prints:
Search for movie: Blithe Spirit
Blithe Spirit DVD is AVAILABLE
The movies
dictionary is:
{
"A Walk Among the Tombstones": "2014",
"Blithe Spirit": "2020",
"Jeepers Creepers": "2001",
"The Place Beyond the Pines": "2012",
}