如何随机排列列表并以 5 项为一组提供

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

我正在尝试编写一个程序,其中的函数将随机播放列表并只给我前 5 首歌曲。然后,当我问用户,你想看更多吗,用户回答是,我希望它打印下 5 首并继续播放,直到没有更多歌曲为止。我已经为此工作了好几天,但被困住了。如果有人可以提供帮助,这是我的代码。

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

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.

根据这个问题陈述,我建议首先使用 random.shuffle 之类的方法随机化播放列表中的整个视频 ID 列表,这样效率更高 - 因为您只需要随机播放列表一次,而不是在列表中生成一个随机索引,然后不断迭代并将其从列表中删除,这也会使其更难阅读。如果目标是打乱播放列表本身的顺序,这种方式应该更容易。

例如:

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)

输出(每次顺序应该不同):

[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]

您可以制作一个给出 5 个块的迭代器,并在需要时使用 next() 函数获取下一个块:

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]