Home > database >  How to shuffle a list randomly and provide it then in chunks of 5 items
How to shuffle a list randomly and provide it then in chunks of 5 items

Time:11-13

I am trying to write a program where the function will shuffle through the list and only give me the first 5 songs. Then when I ask the user, do you want to see more and the user replies yes, I want it to print the next 5 and keep going until there are no more songs left. I have been working on this for days and am stuck. Here is my code if anyone can help.

video_id_to_title = {
    5390161: "Who Want Smoke",
    7736243: "INDUSTRY BABY",
    8267507: "STAY",
    1012930: "Style",
    1109274: "bad guy",
    2981023: "Blank Space",
    4922599: "Love Nwantiti Remix",
    4559658: "Essence (Official Video)",
    9897626: "Pepas",
    5610524: "Outside (Better Days)",
    6980497: "Lo Siento BB:/",
    4547223: "Face Off",
    9086699: "Heat Waves",
    3720918: "Despacito",
    9086691: "Royals",
    1461025: "Fancy Like",
    7434914: "Way 2 Sexy",
    6093037: "Corta Venas",
    6438692: "Need to Know",
    8117542: "MONEY",
    5746821: "Wild Side ",
    9566779: "Knife Talk",
    1683724: "Life Support",
    5718817: "Save Your Tears",
    2459304: "Ghost",
    6382983: "Love Yourself",
    7394792: "7 rings",
}


top_hits_playlist = [
    5390161, 7736243, 8267507, 4922599, 4559658, 9897626, 1461025, 5746821,
    9566779, 5718817, 2459304, 6382983, 7394792
]

def display_full_playlist(playlist_id: int):
   user_playlist_choice = input("Which playlists do you want to see? ")
   answer = input("Do you want to see more?")
   song = 5
   if user_playlist_choice == "Top Hits" or "top hits":
    for i,x in enumerate(top_hits_playlist):
     print(video_id_to_title[x])
     if song == x:
      print(answer)
    if answer == "yes" or answer == "Yes":
      print()
      song  = 5

CodePudding user response:

I am trying to write a program where the function will shuffle through the list and only give me the first 5 songs. Then when I ask the user, do you want to see more and the user replies yes, I want it to print the next 5 and keep going until there are no more songs left.

Based on this problem statement, I'd suggest randomizing the entire list of video id's in the playlist first using something like random.shuffle, just so its more efficient - since you only need to shuffle the list once, rather than generating a random index in the list and then iterating over and removing it from the list continuously, which can also make it harder to read. This way it should be easier, if the goal is to shuffle the order of the playlist itself.

For example:

import random

video_id_to_title = {
    5390161: "Who Want Smoke",
    7736243: "INDUSTRY BABY",
    8267507: "STAY",
    1012930: "Style",
    1109274: "bad guy",
    2981023: "Blank Space",
    4922599: "Love Nwantiti Remix",
    4559658: "Essence (Official Video)",
    9897626: "Pepas",
    5610524: "Outside (Better Days)",
    6980497: "Lo Siento BB:/",
    4547223: "Face Off",
    9086699: "Heat Waves",
    3720918: "Despacito",
    9086691: "Royals",
    1461025: "Fancy Like",
    7434914: "Way 2 Sexy",
    6093037: "Corta Venas",
    6438692: "Need to Know",
    8117542: "MONEY",
    5746821: "Wild Side ",
    9566779: "Knife Talk",
    1683724: "Life Support",
    5718817: "Save Your Tears",
    2459304: "Ghost",
    6382983: "Love Yourself",
    7394792: "7 rings",
}

video_ids = list(video_id_to_title)

# Shuffle the list, so all the elements are randomized
random.shuffle(video_ids)

# Set chunk size, so we get x videos from the playlist at a time
chunk_size = 5

# Split list into sub-lists of at most 5 elements each
video_id_chunks = [video_ids[x:x   chunk_size]
                   for x in range(0, len(video_ids), chunk_size)]

# Print each sub-list with randomized video ids
for chunk in video_id_chunks:
    print(chunk)

Output (which should be in a different order each time):

[1012930, 4547223, 7394792, 7736243, 9086691]
[6980497, 2459304, 8117542, 7434914, 9566779]
[5390161, 6093037, 6438692, 4559658, 2981023]
[8267507, 4922599, 9086699, 5610524, 6382983]
[1683724, 1461025, 9897626, 1109274, 5746821]
[3720918, 5718817]

CodePudding user response:

from random import shuffle

video_id_to_title = {
    5390161: "Who Want Smoke",
    7736243: "INDUSTRY BABY",
    8267507: "STAY",
    1012930: "Style",
    1109274: "bad guy",
    2981023: "Blank Space",
    4922599: "Love Nwantiti Remix",
    4559658: "Essence (Official Video)",
    9897626: "Pepas",
    5610524: "Outside (Better Days)",
    6980497: "Lo Siento BB:/",
    4547223: "Face Off",
    9086699: "Heat Waves",
    3720918: "Despacito",
    9086691: "Royals",
    1461025: "Fancy Like",
    7434914: "Way 2 Sexy",
    6093037: "Corta Venas",
    6438692: "Need to Know",
    8117542: "MONEY",
    5746821: "Wild Side ",
    9566779: "Knife Talk",
    1683724: "Life Support",
    5718817: "Save Your Tears",
    2459304: "Ghost",
    6382983: "Love Yourself",
    7394792: "7 rings"
}

# get song id's in a random order
top_hits_playlist = list(video_id_to_title.keys())
shuffle(top_hits_playlist)
def get_next_n_song(n):
    return [video_id_to_title[(top_hits_playlist.pop(0))] for _ in range(min(len(top_hits_playlist), n))]

if __name__ == "__main__":
    user_playlist_choice = input("\nWhich playlists do you want to see?\n\t")
    if "top hits" in user_playlist_choice.lower():
        while True:
            songs = get_next_n_song(5)
            print('\n'.join(songs))
            if len(songs) < 5: break
            answer = input("\nDo you want to see more?\n\t")
            if "no" in answer.lower(): break

CodePudding user response:

You can make an iterator that gives chunks of 5 and use the next() function to get the next chunk when needed:

import random
top_hits_playlist = [
    5390161, 7736243, 8267507, 4922599, 4559658, 9897626, 1461025, 5746821,
    9566779, 5718817, 2459304, 6382983, 7394792
]

random.shuffle(top_hits_playlist)
get5 = ( top_hits_playlist[i:i 5] for i in range(0,len(top_hits_playlist),5) )

songs = next(get5,None)
while songs:
    print(songs)
    songs = next(get5,None)
    if not songs: break
    input('5 more')

    
[9566779, 7394792, 5390161, 5746821, 6382983]
5 more
[7736243, 8267507, 5718817, 1461025, 2459304]
5 more
[9897626, 4922599, 4559658]
  • Related