from top_songs_2018 import * import json import time """ What data structure is top_songs? ANSWER HERE What data structure is each song in top_songs? ANSWER HERE What are some differences between lists and dictionaries? """ """ Get the number of songs each artists has in the top songs list Implemention create an empty dictionary called artists loop through top songs for each song artist, check if the artist is already in artists if the artist is NOT in artists, add artist and set the value to 1 if the artist is ALREADY in artists, update the value by incrementing it by 1 """ artists = {} for song in top_songs: artist = song['artists'] if artists.get(artist): artists[artist] += 1 else: artists[artist] = 1 print(artists) """ Check you did it correctly by uncommenting the code below assert statements will give an AssertionError if the condition returns false if the condition returns true nothing will print out which means the tests pass """ assert(artists['Drake'] == 4) assert(artists['David Guetta'] == 1) assert(artists['Selena Gomez'] == 2) assert(artists['Post Malone'] == 6) """ write an assert function that gives an AssertionError, then comment it out """ # assert(artists['G-Eazy'] == 3) """ Computer the average number of songs each artist has in the top songs list and print it out """ total_songs = 0 for artist in artists: total_songs += artists[artist] print(total_songs/len(artists)) """ Append all artists with more than 1 song in the top songs list in the list, top_artists """ top_artists = [] for artist in artists: if artists[artist] > 1: top_artists.append(artist) print(top_artists) # uncommment the assert statement to check your work assert(len(top_artists) == 18) """ Of the artists in the top_artists list, find the song that is most danceable Print out the danceability score, the name of the song, and the name of the artist """ max_dance = 0 dance_name = '' dance_artist = '' start = time.time_ns() for artist in top_artists: for song in top_songs: if song['artists'] == artist: if song['danceability'] > max_dance: max_dance = song['danceability'] dance_name = song['name'] dance_artist = song['artists'] time1 = time.time_ns() - start print(f"{time1} ns") print(max_dance, dance_name, dance_artist) """ How could the music metadata be stored to optimize this search? ANSWER HERE """ """ Find the song with the highest danceability from all of top_songs Print out the danceability score, the name of the song, and the name of the artist """ max_dance = 0 dance_name = '' dance_artist = '' for song in top_songs: if song['danceability'] > max_dance: max_dance = song['danceability'] dance_name = song['name'] dance_artist = song['artists'] print(max_dance, dance_name, dance_artist) """ Print out the average length of a song in seconds """ durations = 0 for song in top_songs: durations += song['duration_ms'] print(f"avg duration of song: {durations/len(top_songs)/60000} seconds") """ Create a new dictionary using top_songs such that each artist is a key and the value for each key (artist) is a list containing the metadata about each of their songs artists name should only appear once as the key of top_songs_dict """ top_songs_dict = {} for song in top_songs: artist = song['artists'] if top_songs_dict.get(artist): song_dict = {} for key, val in song.items(): if key != 'artists': song_dict[key] = val top_songs_dict[artist].append(song_dict) else: song_dict = {} for key, val in song.items(): if key != 'artists': song_dict[key] = val top_songs_dict[artist] = [song_dict] # uncomment the assert statement to check your code assert(len(top_songs_dict['Drake']) == 4) f = open("top_songs_2018_by_artist.txt", 'w') f.write(json.dumps(top_songs_dict, indent = 4)) f.close() """ Now your data is organized so you can easily access songs by an artist """ """ Rewrite your code using top_songs_dict Of the artists in the top_artists list, find the song that is most danceable Print out the danceability score, the name of the song, and the name of the artist """ max_dance = 0 dance_name = '' dance_artist = '' start = time.time_ns() for artist in top_artists: for song in top_songs_dict[artist]: if song['danceability'] > max_dance: max_dance = song['danceability'] dance_name = song['name'] dance_artist = artist time2 = time.time_ns() - start print(f"{time2} ns") print(max_dance, dance_name, dance_artist)