我有一本字典Songs,以歌曲为键,以歌曲的情绪为值。我还有一本Users字典,里面有听过的歌曲。我现在想检查字典中的哪些情绪Songs对每个用户来说最流行,并将这些情绪放入一个新的字典中,将用户与该情绪耦合起来。这就是我现在所拥有的:
from collections import Counter
Songs = {
'Song1' : ['techno', 'upbeat'],
'Song2' : ['rock', 'upbeat'],
'Song3' : ['pop', 'sad'],
'Song5' : ['pop', 'calm'],
'Song6' : ['rap', 'upbeat'],
'Song7' : ['rock', 'sad']
}
Users = {
'User1' : ['Song1', 'Song2', 'Song6'],
'User2' : ['Song3', 'Song7'],
'User3' : ['Song2', 'Song7']
}
Users_moods = dict.fromkeys(Users)
for user, song in Users.items():
for song, mood in Songs.items():
mood = set(mood)
mood_counter = Songs[song]
Users_moods = {user: counter.most_common(1)[0][0] for user, counter in Users.items()}
print(Users_moods)
print(Songs)
print(Users)
但它给了我错误。Users_mood对于此示例,字典应如下所示:
Users_moods = {
'User1' : 'upbeat',
'User2' : 'sad',
'User3' : 'rock'
}
GCT1015
喵喵时光机
慕斯王
相关分类