Skip to content

Commit 7ac4b11

Browse files
committed
Day 5
1 parent 3105381 commit 7ac4b11

File tree

3 files changed

+5142
-38
lines changed

3 files changed

+5142
-38
lines changed

days/04-06-collections/collections.ipynb

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@
3030
},
3131
{
3232
"cell_type": "code",
33-
"execution_count": 1,
33+
"execution_count": 2,
3434
"metadata": {
3535
"ExecuteTime": {
36-
"end_time": "2023-11-15T16:02:04.068436300Z",
37-
"start_time": "2023-11-15T16:02:04.053114200Z"
36+
"end_time": "2023-11-17T01:06:31.770445700Z",
37+
"start_time": "2023-11-17T01:06:31.745630600Z"
3838
}
3939
},
4040
"outputs": [],
@@ -600,16 +600,19 @@
600600
},
601601
{
602602
"cell_type": "code",
603-
"execution_count": 21,
604-
"metadata": {},
603+
"execution_count": 3,
604+
"metadata": {
605+
"ExecuteTime": {
606+
"end_time": "2023-11-17T01:06:46.790591300Z",
607+
"start_time": "2023-11-17T01:06:46.289664200Z"
608+
}
609+
},
605610
"outputs": [
606611
{
607612
"data": {
608-
"text/plain": [
609-
"('movies.csv', <http.client.HTTPMessage at 0x10fee00f0>)"
610-
]
613+
"text/plain": "('movies.csv', <http.client.HTTPMessage at 0x264400ceba0>)"
611614
},
612-
"execution_count": 21,
615+
"execution_count": 3,
613616
"metadata": {},
614617
"output_type": "execute_result"
615618
}
@@ -629,8 +632,13 @@
629632
},
630633
{
631634
"cell_type": "code",
632-
"execution_count": 22,
633-
"metadata": {},
635+
"execution_count": 4,
636+
"metadata": {
637+
"ExecuteTime": {
638+
"end_time": "2023-11-17T01:07:09.958624300Z",
639+
"start_time": "2023-11-17T01:07:09.910768Z"
640+
}
641+
},
634642
"outputs": [],
635643
"source": [
636644
"Movie = namedtuple('Movie', 'title year score')"
@@ -645,8 +653,13 @@
645653
},
646654
{
647655
"cell_type": "code",
648-
"execution_count": 23,
649-
"metadata": {},
656+
"execution_count": 5,
657+
"metadata": {
658+
"ExecuteTime": {
659+
"end_time": "2023-11-17T01:09:29.726516600Z",
660+
"start_time": "2023-11-17T01:09:29.724008300Z"
661+
}
662+
},
650663
"outputs": [],
651664
"source": [
652665
"def get_movies_by_director(data=movies_csv):\n",
@@ -671,8 +684,13 @@
671684
},
672685
{
673686
"cell_type": "code",
674-
"execution_count": 24,
675-
"metadata": {},
687+
"execution_count": 6,
688+
"metadata": {
689+
"ExecuteTime": {
690+
"end_time": "2023-11-17T01:09:47.390147900Z",
691+
"start_time": "2023-11-17T01:09:47.326915600Z"
692+
}
693+
},
676694
"outputs": [],
677695
"source": [
678696
"directors = get_movies_by_director()"
@@ -687,23 +705,19 @@
687705
},
688706
{
689707
"cell_type": "code",
690-
"execution_count": 25,
691-
"metadata": {},
708+
"execution_count": 7,
709+
"metadata": {
710+
"ExecuteTime": {
711+
"end_time": "2023-11-17T01:10:09.726082200Z",
712+
"start_time": "2023-11-17T01:10:09.703105300Z"
713+
}
714+
},
692715
"outputs": [
693716
{
694717
"data": {
695-
"text/plain": [
696-
"[Movie(title='The Dark Knight Rises', year=2012, score=8.5),\n",
697-
" Movie(title='The Dark Knight', year=2008, score=9.0),\n",
698-
" Movie(title='Interstellar', year=2014, score=8.6),\n",
699-
" Movie(title='Inception', year=2010, score=8.8),\n",
700-
" Movie(title='Batman Begins', year=2005, score=8.3),\n",
701-
" Movie(title='Insomnia', year=2002, score=7.2),\n",
702-
" Movie(title='The Prestige', year=2006, score=8.5),\n",
703-
" Movie(title='Memento', year=2000, score=8.5)]"
704-
]
718+
"text/plain": "[Movie(title='The Dark Knight Rises', year=2012, score=8.5),\n Movie(title='The Dark Knight', year=2008, score=9.0),\n Movie(title='Interstellar', year=2014, score=8.6),\n Movie(title='Inception', year=2010, score=8.8),\n Movie(title='Batman Begins', year=2005, score=8.3),\n Movie(title='Insomnia', year=2002, score=7.2),\n Movie(title='The Prestige', year=2006, score=8.5),\n Movie(title='Memento', year=2000, score=8.5)]"
705719
},
706-
"execution_count": 25,
720+
"execution_count": 7,
707721
"metadata": {},
708722
"output_type": "execute_result"
709723
}
@@ -725,20 +739,19 @@
725739
},
726740
{
727741
"cell_type": "code",
728-
"execution_count": 26,
729-
"metadata": {},
742+
"execution_count": 8,
743+
"metadata": {
744+
"ExecuteTime": {
745+
"end_time": "2023-11-17T01:11:18.955661900Z",
746+
"start_time": "2023-11-17T01:11:18.939176400Z"
747+
}
748+
},
730749
"outputs": [
731750
{
732751
"data": {
733-
"text/plain": [
734-
"[('Steven Spielberg', 26),\n",
735-
" ('Woody Allen', 22),\n",
736-
" ('Martin Scorsese', 20),\n",
737-
" ('Clint Eastwood', 20),\n",
738-
" ('Ridley Scott', 17)]"
739-
]
752+
"text/plain": "[('Steven Spielberg', 26),\n ('Woody Allen', 22),\n ('Martin Scorsese', 20),\n ('Clint Eastwood', 20),\n ('Ridley Scott', 17)]"
740753
},
741-
"execution_count": 26,
754+
"execution_count": 8,
742755
"metadata": {},
743756
"output_type": "execute_result"
744757
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from collections import defaultdict, namedtuple, Counter, deque
2+
import csv
3+
import random
4+
from urllib.request import urlretrieve
5+
import os
6+
7+
Movie = namedtuple('Movie', 'title year score gross')
8+
movies_csv = os.path.join(".\\", "movies.csv")
9+
10+
def main():
11+
#movie_data = 'https://raw.githubusercontent.com/pybites/challenges/solutions/13/movie_metadata.csv'
12+
#urlretrieve(movie_data, movies_csv)
13+
14+
directors = get_movies_by_director(movies_csv)
15+
16+
cnt = Counter()
17+
#print(directors.items())
18+
for director, movies in directors.items():
19+
cnt[director] += len(movies)
20+
21+
print(cnt.most_common(5))
22+
23+
24+
def get_movies_by_director(data):
25+
"""Extracts all movies from csv and stores them in a dictionary
26+
where keys are directors, and values is a list of movies (named tuples)"""
27+
directors = defaultdict(list)
28+
with open(data, encoding='utf-8') as f:
29+
for line in csv.DictReader(f):
30+
try:
31+
director = line['director_name']
32+
movie = line['movie_title'].replace('\xa0', '')
33+
year = int(line['title_year'])
34+
score = float(line['imdb_score'])
35+
gross = float(line['gross'])
36+
except ValueError:
37+
continue
38+
39+
m = Movie(title=movie, year=year, score=score, gross=gross)
40+
if m.year >= 1960:
41+
directors[director].append(m)
42+
43+
return directors
44+
45+
46+
if __name__ == "__main__":
47+
main()

0 commit comments

Comments
 (0)