Home > other >  Python Trying to assign 100 values to 100keys with some conditions
Python Trying to assign 100 values to 100keys with some conditions

Time:12-12

I'm new at python so please go easy on me. The users is a dict [str,list[str].The keys are just user1, user2 etc until 100. The list of strings contains 10 songs each. The premade_playlist also has keys premade1, premade2 etc until 100.The values are also a dict [str,list[str] and contains about 50 songs in the list[str].

I am trying to assign a str value to each user, if the premade playlist has 3 songs that the user has in their values and 3 that the user doesnt have in their values. However it keeps giving me only one premade. I am stuck as to what i should do abou this. Any help would be appreciated.

i printed out the premade_for_user and got a dict with all values 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()

CodePudding user response:

i'm not exactly sure what you want to do but i have created this 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)

I hope this helped.

  • Related