0

我想制作一个播放列表生成器,我有一个包含不同歌曲的列表,我想从中生成一个仅包含 10 个项目的随机编号列表,这是我尝试的代码:

import random

a = ['Bruised and Scarred - Mayday Parade',
     'Miracles in December - EXO',
     'All Too Well - Taylor Swift',
     'Gravity - Sara Bareilles',
     'Perfectly Perfect - Simple Plan',
     'Welcome To The Black Parade - My Chemical Romance',
     'Everything Has Changed - Taylor Swift',
     'Champagne - Taylor Swift',
     'Piece of Your Heart - Mayday Parade',
     'Blame It On The Rain - He Is We',
     'Sad Song - We The Kings',
     'Give It All - He Is We',
     ]

for x in range(1, 11):
    for y in random.sample(a, k=10):
        print(str(x) + y)

但我得到了这个输出:

1Bruised and Scarred - Mayday Parade
1All Too Well - Taylor Swift
1Blame It On The Rain - He Is We
1Everything Has Changed - Taylor Swift
1Give It All - He Is We
1Miracles in December - EXO
1Perfectly Perfect - Simple Plan
1Gravity - Sara Bareilles
1Sad Song - We The Kings
1Piece of Your Heart - Mayday Parade
2All Too Well - Taylor Swift
2Bruised and Scarred - Mayday Parade
2Blame It On The Rain - He Is We
2Perfectly Perfect - Simple Plan
2Champagne - Taylor Swift
2Everything Has Changed - Taylor Swift
2Piece of Your Heart - Mayday Parade
2Miracles in December - EXO
2Welcome To The Black Parade - My Chemical Romance
2Give It All - He Is We
3Bruised and Scarred - Mayday Parade
3Gravity - Sara Bareilles
3Welcome To The Black Parade - My Chemical Romance
3All Too Well - Taylor Swift
3Perfectly Perfect - Simple Plan
3Sad Song - We The Kings
3Champagne - Taylor Swift
3Everything Has Changed - Taylor Swift
3Piece of Your Heart - Mayday Parade
3Give It All - He Is We
4Everything Has Changed - Taylor Swift
4Blame It On The Rain - He Is We
4Piece of Your Heart - Mayday Parade
4Gravity - Sara Bareilles
4Bruised and Scarred - Mayday Parade
4Welcome To The Black Parade - My Chemical Romance
4Miracles in December - EXO
4Sad Song - We The Kings
4Give It All - He Is We
4Perfectly Perfect - Simple Plan
5Gravity - Sara Bareilles
5Blame It On The Rain - He Is We
5Perfectly Perfect - Simple Plan
5Champagne - Taylor Swift
5Everything Has Changed - Taylor Swift
5Bruised and Scarred - Mayday Parade
5Welcome To The Black Parade - My Chemical Romance
5All Too Well - Taylor Swift
5Give It All - He Is We
5Miracles in December - EXO
6Bruised and Scarred - Mayday Parade
6Champagne - Taylor Swift
6Everything Has Changed - Taylor Swift
6Miracles in December - EXO
6Welcome To The Black Parade - My Chemical Romance
6Sad Song - We The Kings
6All Too Well - Taylor Swift
6Gravity - Sara Bareilles
6Give It All - He Is We
6Perfectly Perfect - Simple Plan
7Gravity - Sara Bareilles
7Sad Song - We The Kings
7Everything Has Changed - Taylor Swift
7Welcome To The Black Parade - My Chemical Romance
7Piece of Your Heart - Mayday Parade
7Blame It On The Rain - He Is We
7Bruised and Scarred - Mayday Parade
7Give It All - He Is We
7Champagne - Taylor Swift
7All Too Well - Taylor Swift
8Sad Song - We The Kings
8Gravity - Sara Bareilles
8Champagne - Taylor Swift
8Blame It On The Rain - He Is We
8Miracles in December - EXO
8Give It All - He Is We
8Welcome To The Black Parade - My Chemical Romance
8Bruised and Scarred - Mayday Parade
8Piece of Your Heart - Mayday Parade
8Everything Has Changed - Taylor Swift
9Gravity - Sara Bareilles
9Champagne - Taylor Swift
9Bruised and Scarred - Mayday Parade
9Blame It On The Rain - He Is We
9Piece of Your Heart - Mayday Parade
9Everything Has Changed - Taylor Swift
9Sad Song - We The Kings
9Welcome To The Black Parade - My Chemical Romance
9All Too Well - Taylor Swift
9Perfectly Perfect - Simple Plan
10Champagne - Taylor Swift
10Blame It On The Rain - He Is We
10Perfectly Perfect - Simple Plan
10Miracles in December - EXO
10Give It All - He Is We
10Piece of Your Heart - Mayday Parade
10Everything Has Changed - Taylor Swift
10Welcome To The Black Parade - My Chemical Romance
10Bruised and Scarred - Mayday Parade
10Sad Song - We The Kings

我希望它只包含 10 个项目,但我得到的太多了,请帮帮我(抱歉我的英语不好)

4

2 回答 2

4

你得到那个输出是因为你有一个迭代十次的外循环和一个也迭代十次的内循环。所以你得到十个1,然后是十个2,等等。

你不需要外range()循环。仅遍历歌曲选择,enumerate()用于获取项目位置和歌曲标题:

for position, title in enumerate(random.sample(a, k=10)):
    print(str(position) + title)
于 2020-12-26T01:47:34.973 回答
2

只需删除您的外循环。

嵌套循环将为您提供 10 x 10 行的输出。

您的调用random.sample()已经产生了您需要的 10 行(随机)输出。

此外,用于enumerate迭代并获取歌曲索引,从 1 开始。

尝试这个:

import random

a = [
    'Bruised and Scarred - Mayday Parade',
    'Miracles in December - EXO',
    'All Too Well - Taylor Swift',
    'Gravity - Sara Bareilles',
    'Perfectly Perfect - Simple Plan',
    'Welcome To The Black Parade - My Chemical Romance',
    'Everything Has Changed - Taylor Swift',
    'Champagne - Taylor Swift',
    'Piece of Your Heart - Mayday Parade',
    'Blame It On The Rain - He Is We',
    'Sad Song - We The Kings',
    'Give It All - He Is We',
]

for i, y in enumerate(random.sample(a, k=10), 1):
    print(i, y)
于 2020-12-26T01:48:41.577 回答