-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImdb.py
More file actions
54 lines (49 loc) · 2.41 KB
/
Copy pathImdb.py
File metadata and controls
54 lines (49 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# Write a function called imdb_dictionary. imdb_dictionary
# should have one parameter, a string representing a
# filename.
#
# On each row of the file will be a performer's name, then
# a semicolon, then a comma-separated list of movies the have
# been in. For example, one file's contents could be:
#
# Robert Downey Jr.; Avengers: Infinity War, Sherlock Holmes 3, Spider-Man: Homecoming
# Scarlett Johansson; Avengers: Infinity War, Isle of Dogs, Ghost in the Shell
# Elizabeth Olsen; Avengers: Infinity War, Kodachrome, Wind River, Ingrid Goes West
#
# You may assume that the only semi-colon will be after the
# performer's name, and that there will be no commas in the
# movie titles.
#
# Return a dictionary where the keys are each actor's name,
# and the values are alphabetically-sorted lists of the movies
# they have been in. For example, if imdb_dictionary was called
# on the file above, the output would be:
# {"Robert Downey Jr.": ["Avengers: Infinity War", "Sherlock Holmes 3", "Spider-Man: Homecoming"],
# "Scarlett Johansson": ["Avengers: Infinity War", "Ghost in the Shell", "Isle of Dogs"],
# Elizabeth Olsen": ["Avengers: Infinity War", "Ingrid Goes West", "Kodachrome", "Wind River"]}
#
# Make sure the list of movies is sorted alphabetically. Don't
# worry about the order the keys (names) appear in the dictionary.
# Add your code here!
def imdb_dictionary(filename):
lines = open(filename, "r")
movie_dict = {}
for line in lines:
line = line.strip()
line = line.split("; ")
name, movie_title = line
movie_title = movie_title.split(", ")
movie_title.sort()
# new_movie_title = ", ".join(movie_title)
# movie_dict[name] = []
movie_dict[name] = movie_title
lines.close()
return movie_dict
# Below are some lines of code that will test your function.
# You can change the contents of some_performers.txt from
# the dropdown in the top left to test other inputs.
#
# If your function works correctly, this will originally
# print (although the order of the keys may vary):
# {"Robert Downey Jr.": ["Avengers: Infinity War", "Sherlock Holmes 3", "Spider-Man: Homecoming"], "Scarlett Johansson": ["Avengers: Infinity War", "Ghost in the Shell", "Isle of Dogs"], Elizabeth Olsen": ["Avengers: Infinity War", "Ingrid Goes West", "Kodachrome", "Wind River"]}
print(imdb_dictionary("some_performers.txt"))