So I am sorting a dictionary by having the key as the primary Genre and the value as the total number of albums in each Genre, and I cannot get the key=lambda part to output correctly, everything I have tried just outputs a blank Number of Albums.
I have tried key=lambda x:x[1] but outputs blank answers and I tried a couple other things which have not worked. Below is my code so far, the only thing as far as I am concerned is incorrect is what to put after key=lambda. I marked the part below with ** **
INPUT
import csv
def AlbumGenreAnalysis(fileName):
myFile = open(fileName, "r")
csvReader = csv.reader(myFile)
header = next(csvReader)
genreDictionary = {}
for row in csvReader:
firstWord = row[4].split(",")
if(firstWord[0] not in genreDictionary):
genreDictionary[firstWord[0]] = 1
else:
genreDictionary[firstWord[0]] = 1
return genreDictionary
def sortDictionaryByValues(dictionaryName, descending):
if (type(descending) != bool and type(dictionaryName) != dict):
raise ValueError("sortDictionaryByValues requires a dictionary and boolean as arguments, respectively")
return None
else:
sortedDictionary = {}
sortedList = sorted(dictionaryName.items(),**key=lambda ...**,reverse=descending)
for item in sortedList:
sortedDictionary[item[0]] = item[1]
print("Rolling Stone Top 500 Albums Genre Analysis")
print("Genre Name\t\tNumber Of Albums")
for row in sortedDictionary:
print(row+str(sortedDictionary[row]))
sortDictionaryByValues(AlbumGenreAnalysis("RollingStoneAlbumList.csv"),descending=True)
OUTPUT
Rolling Stone Top 500 Albums Genre Analysis
Genre Name Number Of Albums
Rock1
Reggae1
Pop1
Latin1
Jazz1
Hip Hop1
Funk / Soul1
Folk1
Electronic1
Classical1
Blues1