繁体   English   中英

如何随机打乱一个列表,然后以 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

从 Python 3.7 版开始,字典是有序的。 在 Python 3.6 及更早版本中,字典是无序的。

当我们说字典是有序的时,这意味着项目有一个定义的顺序,并且这个顺序不会改变。

无序意味着项目没有定义的顺序,您不能使用索引来引用项目。

更多信息: https : //www.w3schools.com/python/python_dictionaries.asp

我正在尝试编写一个程序,该函数将在列表中随机播放,并且只给我前 5 首歌曲。 然后当我问用户,你想看更多吗,用户回答是,我想让它打印接下来的 5 首并继续下去,直到没有更多的歌曲剩下。

基于此问题陈述,我建议首先使用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]
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

您可以创建一个迭代器,提供 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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM