当我使用随机时 python 如何打印每个元素?
how python print me each element when i use random?
我需要一个包含 8 个元素的列表,我希望 python 随机导入列表中的元素。但我需要列表中的每个元素。像这样:
我需要列表中的数字 0 到 4,但是如果我写:
s = []
for i in range(8):
s.append(random.randint(0,4))
print("s:", s)
python 不会将每个数字至少打印一次。 Python 像这样打印我:
s = [1,0,2,2,1,0,1,3]
- 在此列表中缺少 4 个,但我希望所有 5 个数字至少在列表中出现一次。
请帮帮我
如果你想要一个包含八个项目的列表,其中至少包含一个元素 0,1,2,3,4,那么你真正想要的是 [0,1,2 ,3,4] 和三个额外的随机元素,全部以随机顺序排列:
import random
# start a list with one each of the desired elements
s = [0,1,2,3,4]
# add three more elements
for i in range(3):
s.append(random.randint(0,4))
# randomize the order of the elements in the list
random.shuffle(s)
print("s:", s)
我需要一个包含 8 个元素的列表,我希望 python 随机导入列表中的元素。但我需要列表中的每个元素。像这样:
我需要列表中的数字 0 到 4,但是如果我写:
s = []
for i in range(8):
s.append(random.randint(0,4))
print("s:", s)
python 不会将每个数字至少打印一次。 Python 像这样打印我:
s = [1,0,2,2,1,0,1,3]
- 在此列表中缺少 4 个,但我希望所有 5 个数字至少在列表中出现一次。
请帮帮我
如果你想要一个包含八个项目的列表,其中至少包含一个元素 0,1,2,3,4,那么你真正想要的是 [0,1,2 ,3,4] 和三个额外的随机元素,全部以随机顺序排列:
import random
# start a list with one each of the desired elements
s = [0,1,2,3,4]
# add three more elements
for i in range(3):
s.append(random.randint(0,4))
# randomize the order of the elements in the list
random.shuffle(s)
print("s:", s)