Untitled
unknown
python
3 years ago
4.4 kB
6
Indexable
def display_list(list2D):
'''input: list data structure
return: None
Prints elements of a 2D list to the terminal
'''
if list2D == []:
print("List is empty")
for row in list2D:
print(row)
#-------------------------------------------------------------------
def display_dict(adict):
'''input: dictionary data structure
return: None
Prints elements of a dictionary in sorted order of keys to the terminal
'''
if adict == {}:
print("Dictionary is empty")
for key in sorted(adict):
print(str(key) + ": " +str(adict[key]))
#-------------------------------------------------------------------
# add functions here
def load_data(movies, ratings, tags):
movie_db = []
ratings_db = []
tags_db = []
try:
with open(movies, "r") as m:
m.readline().strip().split(",")
# read all other rows and add elements to a 2D-list record
for line in m:
line = line.strip().split(",")
line[0] = int(line[0])
movie_db.append(line)
except Exception as e:
print(e)
try:
with open(ratings, "r") as r:
r.readline().strip().split(",")
for line in r:
line = line.strip().split(",")
line[0] = int(line[0])
line[1] = int(line[1])
line[2] = float(line[2])
line[3] = int(line[3])
ratings_db.append(line)
except Exception as e:
print(e)
try:
with open(tags, "r") as t:
t.readline().strip().split(",")
for line in t:
line = line.strip().split(",")
line[0] = int(line[0])
line[1] = int(line[1])
line[3] = int(line[3])
tags_db.append(line)
except Exception as e:
print(e)
return movie_db, ratings_db, tags_db
#-------------------------------------------------------------------
def get_genre_dict(db):
genres = {}
for movie in db:
movie_id = movie[0]
genre = movie[2]
genre_split = genre.split("|")
for genre in genre_split:
if genre not in genres:
genres[genre] = [movie_id]
else:
genres[genre].append(movie_id)
return genres
#-------------------------------------------------------------------
def get_tag_dict(db):
tags = {}
for movie in db:
movie_id = movie[1]
tag = movie[2]
if tag not in tags:
tags[tag] = [movie_id]
else:
tags[tag].append(movie_id)
return tags
#-------------------------------------------------------------------
def get_avg_ratings(db):
avg_ratings = {}
for movie in db:
movie_id = movie[1]
rating = movie[2]
if movie_id not in avg_ratings:
avg_ratings[movie_id] = [rating]
else:
avg_ratings[movie_id].append(rating)
for avg in avg_ratings:
avg_ratings[avg] = round(sum(avg_ratings[avg]) / len(avg_ratings[avg]), 2)
return avg_ratings
#-------------------------------------------------------------------
def get_movie_dict(movie_db, ratings):
movies = {}
for movie in movie_db:
movie_id = movie[0]
movie_title = movie[1][:-7]
year = int(movie[1][-5:-1])
avg_rating = ratings[movie_id]
movies [movie_id] = [movie_title, year, avg_rating]
return movies
#-------------------------------------------------------------------
# Test other functions in main()
def main():
# testing display_list()
alist = [[1,2],[3,4],[5],[6,7]]
display_list(alist)
# testing display_dict()
adict = {2:6, 7:1, 4:3, 5:"zero"}
display_dict(adict)
# test your functions here
# filename1 = "movies_20.csv"
# filename2 = "ratings_20.csv"
# filename3 = "tags_20.csv"
# testing load_data()
# movies_db, ratings_db, tags_db = load_data(filename1, filename2, filename3)
# display_list(movies_db[2:5])
# display_list(ratings_db[2:5])
# display_list(tags_db[2:5])
# testing get_genre_dict()
# genres = get_genre_dict(movies_db)
# display_dict(genres)
# testing get_avg_ratings()
# ratings = get_avg_ratings(ratings_db)
# display_dict(ratings)
# testing get_movie_dict()
movies = get_movie_dict(movies_db, ratings)
display_dict(movies)
# testing get_tag_dict()
# tags = get_tag_dict(tags_db)
# display_dict(tags)
# calls main()
if __name__ == "__main__":
main()
Editor is loading...