Untitled

 avatar
unknown
python
2 years ago
3.4 kB
2
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[rating].append(movie_id)
        
  return avg_ratings

#-------------------------------------------------------------------

# Test other functions in main()
def main():
    movies_db, ratings_db, tags_db = load_data("movies_20.csv", "ratings_20.csv", "tags_20.csv")
    # display_list(movies_db[2:5])
    # display_list(ratings_db[2:5])
    # display_list(tags_db[2:5])
    # ratings = get_avg_ratings(ratings_db)
    # display_dict(ratings)

    # testing get_avg_ratings()
    ratings = get_avg_ratings(ratings_db)
    display_dict(ratings)
# calls main()
if __name__ == "__main__":
    main()
Editor is loading...