@@ -22,25 +22,6 @@ def get_movies_by_director():
22
22
"""Extracts all movies from csv and stores them in a dict,
23
23
where keys are directors, and values are a list of movies,
24
24
use the defined Movie namedtuple"""
25
-
26
- # ""
27
- # Their code:
28
- #
29
- #
30
- # directors = defaultdict(list)
31
- # with open(data, encoding='utf-8') as f:
32
- # for line in csv.DictReader(f):
33
- # try:
34
- # director = line['director_name']
35
- # movie = line['movie_title'].replace('\xa0', '')
36
- # year = int(line['title_year'])
37
- # score = float(line['imdb_score'])
38
- # except ValueError:
39
- # continue
40
- #
41
- # m = Movie(title=movie, year=year, score=score)
42
- # directors[director].append(m)
43
- # ""
44
25
movies = defaultdict (list )
45
26
input_file = csv .DictReader (open (f"/tmp/{ fname } " ))
46
27
for row in input_file :
@@ -53,7 +34,6 @@ def get_movies_by_director():
53
34
return movies
54
35
55
36
56
-
57
37
def calc_mean_score (movies ):
58
38
"""Helper method to calculate mean of list of Movie namedtuples,
59
39
round the mean to 1 decimal place"""
@@ -80,30 +60,20 @@ def get_average_scores(directors):
80
60
if len (directors [director ]) < 4 :
81
61
director_del .append (director )
82
62
83
- for dir in director_del :
84
- del directors [dir ]
85
-
86
- returnValue = []
63
+ for direct in director_del :
64
+ del directors [direct ]
87
65
88
- # for director, movies in directors.items():
89
- # total = 0
90
- # count = 0
91
- # for mov in movies:
92
- # total = total + float(mov.score[1])
93
- # count = count + 1
94
- # returnValue.append((director, round((total / count), 1)))
66
+ return_value = []
95
67
96
68
for director , movies in directors .items ():
97
- returnValue .append (
69
+ return_value .append (
98
70
(
99
71
director ,
100
72
round ((sum ([float (mov .score [1 ]) for mov in movies ]) / len (movies )) , 1 )
101
73
)
102
74
)
103
75
104
- return sorted (returnValue , key = lambda x : x [1 ], reverse = True )
105
-
106
-
76
+ return sorted (return_value , key = lambda x : x [1 ], reverse = True )
107
77
108
78
109
79
director_movies = get_movies_by_director ()
0 commit comments