Advertisement
1sairandhri

favorite genres

Apr 3rd, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. #Input:
  2. userSongs = {  
  3.    "David": ["song1", "song2", "song3", "song4", "song8"],
  4.    "Emma":  ["song5", "song6", "song7"]
  5. }
  6. songGenres = {  
  7.    "Rock":    ["song1", "song3"],
  8.    "Dubstep": ["song7"],
  9.    "Techno":  ["song2", "song4"],
  10.    "Pop":     ["song5", "song6"],
  11.    "Jazz":    ["song8", "song9"]
  12. }
  13.  
  14. """Output: {  
  15.   "David": ["Rock", "Techno"],
  16.   "Emma":  ["Pop"]
  17. }"""
  18. import collections
  19. def favGenres(userSongs, songGenres):
  20.     output={}
  21.     for i in userSongs:
  22.         lst=userSongs[i]
  23.         count=collections.defaultdict(int)
  24.         for j in lst:
  25.             for k,v in songGenres.items():
  26.                 if j in v:
  27.                     count[k]+=1
  28.         print(count)
  29.         output[i]=[key for key,val in count.items() if val == max(count.values())]
  30.     return output
  31. favGenres(userSongs, songGenres)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement