Python 如何从列表中选择5个随机数创建5个列表?

Python 如何从列表中选择5个随机数创建5个列表?,python,Python,从数字列表中,我想随机创建5个数字列表 import random def team(): x = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] y = [] Count = 0 count = 0 while Count < 5: while count<5: count += 1 z = random.choice(x

从数字列表中,我想随机创建5个数字列表

import random

def team():
    x = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] 
    y = []
    Count = 0
    count = 0
    while Count < 5:
        while count<5:
            count += 1
            z = random.choice(x)
            y += z
            x.remove(z)
        print(y)
        Count +=1

team()
随机导入
def团队():
x=['0','1','2','3','4','5','6','7','8','9']
y=[]
计数=0
计数=0
当计数小于5时:
当计数时,将代码更改为

import random

def team():
    Count = 0
    while Count < 5:
        x = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
        y = []
        count = 0
        while count<5:
            count += 1
            z = random.choice(x)
            y += z
            x.remove(z)
        print(y)
        Count +=1

team()

随机导入
def团队():
计数=0
当计数小于5时:
x=['0','1','2','3','4','5','6','7','8','9']
y=[]
计数=0
当计数时,将代码更改为

import random

def team():
    Count = 0
    while Count < 5:
        x = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
        y = []
        count = 0
        while count<5:
            count += 1
            z = random.choice(x)
            y += z
            x.remove(z)
        print(y)
        Count +=1

team()

随机导入
def团队():
计数=0
当计数小于5时:
x=['0','1','2','3','4','5','6','7','8','9']
y=[]
计数=0

而count如果使用嵌套列表,则这是一行。此外,我们可以直接对整数0..9进行采样,而不是对字符串列表进行采样
['0','1',…,'9']

import random

teams = [[random.randrange(10) for element in range(5)] for count in range(5)]

# Here's one example run: [[0, 8, 5, 6, 2], [6, 7, 8, 6, 6], [8, 8, 6, 2, 2], [0, 1, 3, 5, 8], [7, 9, 4, 9, 6]]
或者,如果您确实想要字符串输出而不是整数:

teams = [[str(random.randrange(10)) for element in range(5)] for count in range(5)]

[['3', '8', '2', '5', '3'], ['7', '1', '9', '7', '9'], ['4', '8', '0', '4', '1'], ['7', '6', '5', '8', '2'], ['6', '9', '2', '7', '3']]
或者,如果您确实希望随机采样任意字符串列表:

[ random.sample(['0','1','2','3','4','5','6','7','8','9'], 5) for count in range(5) ]

[['7', '1', '5', '3', '0'], ['7', '1', '6', '2', '8'], ['3', '0', '9', '7', '2'], ['5', '1', '7', '3', '2'], ['6', '2', '5', '0', '3']]

如果使用嵌套列表,则这是一行。此外,我们可以直接对整数0..9进行采样,而不是对字符串列表进行采样
['0','1',…,'9']

import random

teams = [[random.randrange(10) for element in range(5)] for count in range(5)]

# Here's one example run: [[0, 8, 5, 6, 2], [6, 7, 8, 6, 6], [8, 8, 6, 2, 2], [0, 1, 3, 5, 8], [7, 9, 4, 9, 6]]
或者,如果您确实想要字符串输出而不是整数:

teams = [[str(random.randrange(10)) for element in range(5)] for count in range(5)]

[['3', '8', '2', '5', '3'], ['7', '1', '9', '7', '9'], ['4', '8', '0', '4', '1'], ['7', '6', '5', '8', '2'], ['6', '9', '2', '7', '3']]
或者,如果您确实希望随机采样任意字符串列表:

[ random.sample(['0','1','2','3','4','5','6','7','8','9'], 5) for count in range(5) ]

[['7', '1', '5', '3', '0'], ['7', '1', '6', '2', '8'], ['3', '0', '9', '7', '2'], ['5', '1', '7', '3', '2'], ['6', '2', '5', '0', '3']]

请将所有相关Python代码直接作为文本而不是图像链接包含在问题中。始终将代码和错误消息作为文本而不是图像。在第一个循环中重置
y
count
。这将清除以前的结果,然后您可以有一个新的列表,并且永远不会重复。随机抽样而不替换,对吗?请将所有相关的Python代码作为文本直接包含在您的问题中,而不是作为图像链接。始终将代码和错误消息作为文本而不是图像。在第一个循环中重置
y
count
。这将清除以前的结果,然后你可以有一个新的列表,并且永远不会重复。随机抽样而不替换,对吗?