Python试图在某些条件下为100个键分配100个值



我是python的新手,所以请对我宽容一点。用户是dict [str,list[str]。键就是user1、user2等等,直到100。字符串列表每个包含10首歌曲。premade_playlist也有premade1, premade2等键,直到100。这些值也是一个dict [str,list[str],在list[str]中包含大约50首歌曲。

我试图分配一个str值给每个用户,如果预制播放列表有3首歌曲,用户在他们的值和3,用户没有在他们的值。然而,它只给我一个预制。我不知道该怎么办才好。如有任何帮助,不胜感激。

我打印出premade_for_user并得到一个包含所有值premade1

的字典
premade_for_user:dict[str, list[str]] = {}
for user in users:
for premade in premade_playlists:
listened_to = 0
not_listened_to = 0
while listened_to < 3 and not_listened_to < 3:
for song in premade_playlists[premade]:
if listened_to >= 3 and not_listened_to >= 3:
print("Didn't work?")
elif song in users[user]:
listened_to += 1
elif song not in users[user]:
not_listened_to += 1
else:
print("stuck") 
else:
premade_for_user[user] = premade
break

I printed out the premade_for_user and got a dict with all values premade1. I expect it to be a bit more spread out. The songs were all chosen randomly from a database of 600 songs using random.sample()

我不确定你想做什么,但我已经创建了这个例子:

for user_key, user_value in users.items():
for playlist_key, playlist_value in premade_playlists.items():
listened_to = 0
not_listened_to = 0
for playlist_song in playlist_value:
if playlist_song in user_value:
listened_to += 1
else:
not_listened_to += 1
if listened_to >= 3 and not_listened_to >= 3:
# here all matches of users with playlist that had 3 listened to and 3 not listened to are printed.
print(playlist_key)
print(user_key)

我希望这对你有帮助。

最新更新