1
1
import csv
2
- from collections import defaultdict , namedtuple , OrderedDict , ChainMap
2
+ from collections import defaultdict , namedtuple , OrderedDict , Counter
3
3
from pprint import pprint
4
4
5
5
MOVIE_DATA = "movie_metadata.csv"
@@ -23,17 +23,17 @@ def get_movies_by_director():
23
23
score = float (row ["imdb_score" ])
24
24
except ValueError :
25
25
continue
26
- d [director ].append (Movie (title = title , year = year , score = score ))
26
+ if year > MIN_YEAR :
27
+ d [director ].append (Movie (title = title , year = year , score = score ))
27
28
return d
28
29
29
30
30
31
def get_average_scores (directors ):
31
32
"""Filter directors with < MIN_MOVIES and calculate averge score"""
32
33
fd , dd = {}, {}
33
34
Movie = namedtuple ("Movie" , "score movies" )
34
- for k , v in directors .items ():
35
- if not len (v ) < MIN_MOVIES :
36
- movies = _year_sort (v )
35
+ for k , movies in directors .items ():
36
+ if not len (movies ) < MIN_MOVIES :
37
37
score = _calc_mean (movies )
38
38
fd [k ] = Movie (score = score , movies = movies )
39
39
sd = OrderedDict (
@@ -42,19 +42,16 @@ def get_average_scores(directors):
42
42
return sd
43
43
44
44
45
- def _year_sort (movies ):
46
- for count , movie in enumerate (movies ):
47
- if not movie .year > MIN_YEAR :
48
- del movies [count ]
49
- return movies
50
-
51
-
52
45
def _calc_mean (movies ):
53
46
"""Helper method to calculate mean of list of Movie namedtuples"""
54
- score = 0
55
- for i in movies :
56
- score += i .score
57
- return round (score / len (movies ), 1 )
47
+ return round (sum (i .score for i in movies ) / len (movies ), 1 )
48
+
49
+
50
+ def most_film_directors (directors ):
51
+ cnt = Counter ()
52
+ for director , movies in directors .items ():
53
+ cnt [director ] += len (movies [1 ])
54
+ return cnt .most_common (5 )
58
55
59
56
60
57
def print_results (directors ):
@@ -84,6 +81,7 @@ def main():
84
81
We wrote some tests based on our solution: test_directors.py"""
85
82
directors = get_movies_by_director ()
86
83
directors = get_average_scores (directors )
84
+ # pprint(most_film_directors(directors))
87
85
print_results (directors )
88
86
89
87
0 commit comments