我正在嘗試撰寫一個程式,該函式將在串列中隨機播放,并且只給我前 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
uj5u.com熱心網友回復:
我正在嘗試撰寫一個程式,該函式將在串列中隨機播放,并且只給我前 5 首歌曲。然后當我問用戶,你想看更多嗎,用戶回答是,我想讓它列印接下來的 5 首并繼續下去,直到沒有更多的歌曲剩下。
基于此問題陳述,我建議首先使用類似的方法隨機化播放串列中的整個視頻 ID 串列random.shuffle,這樣效率更高 - 因為您只需要對串列進行一次隨機播放,而不是在串列中生成隨機索引然后不斷迭代并將其從串列中洗掉,這也會使其更難閱讀。如果目標是改變播放串列本身的順序,那么這種方式應該更容易。
例如:
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]
uj5u.com熱心網友回復:
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
uj5u.com熱心網友回復:
您可以創建一個迭代器,提供 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]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/358204.html
上一篇:如果鍵在串列中,則計算串列中的專案并將它們附加到字典中
下一篇:在回圈內更新字典
