计算列表中最常见的项目与字典值列表交叉检查

我有一本字典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'

}


慕无忌1623718
浏览 106回答 3
3回答

GCT1015

def getMoods(user, users, songs):    userSongs = users[user]  # songs this user listens to    moods = {}  # count of each mood this user listens to    for song in userSongs:        for mood in songs[song]:  # for each mood of that song            moods.setdefault(mood, 0)            moods[mood] += 1    return max(moods, key=moods.get)  # the most prevalent moodfor user in users:    print(user, 'likes', getMoods(user, users, songs))  # mood with the highest count

喵喵时光机

您实际上并没有使用Counter并尝试调用most_common列表。让我们尝试将其分解:创建一个新字典,并为每个用户找到其相关的情绪(总体)。从该词典中找到每个用户最常见的情绪。为了更好地处理 1,我们可以使用defaultdict:from collections import Counter, defaultdictUsers_moods = defaultdict(list)for user, songs in Users.items():    for song in songs:        Users_moods[user].extend(Songs[song])common_moods = {user: Counter(moods).most_common(1)[0][0] for user, moods in Users_moods.items()}print(Users_moods)print(common_moods)这给出:defaultdict(<class 'list'>, {'User1': ['techno', 'upbeat', 'rock', 'upbeat', 'rap', 'upbeat'], 'User2': ['pop', 'sad', 'rock', 'sad'], 'User3': ['rock', 'upbeat', 'rock', 'sad']}){'User1': 'upbeat', 'User2': 'sad', 'User3': 'rock'}common或者,您可以使用相同的循环动态构建字典,为Counter每个用户创建一个单独的字典:common_moods = {}for user, songs in Users.items():    User_moods = Counter()    for song in songs:        User_moods += Counter(Songs[song])    common_moods[user] = User_moods.most_common(1)[0][0]print(common_moods)

慕斯王

如果您打算使用 collections.Counter,让我们看看https://docs.python.org/3/library/collections.html#collections.Counter您将需要mood_counter&nbsp;=&nbsp;Counter(iterable)据推测,这里的可迭代来自于查看一个用户,然后是他们的所有歌曲,然后是这些歌曲的所有情绪。我们不要尝试制作一个衬垫,而是正常地迭代这些衬垫。def get_user_mood(user, Users, Songs):&nbsp; &nbsp; accumulated_moods = []&nbsp; &nbsp; for song_name in Users[user]:&nbsp; &nbsp; &nbsp; &nbsp; moods_for_this_song = Songs[song_name]&nbsp; &nbsp; &nbsp; &nbsp; accumulated_moods.extend(moods_for_this_song)&nbsp; &nbsp; mood_counter = Counter(accumulated_moods)&nbsp; &nbsp; return mood_counter.most_common(1)[0][0] # validate?这使您可以在理解中相当轻松地构建字典Users_moods = {user: get_user_mood(user, Users, Songs) for user in Users}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python