按lin输出随机数

2024-04-28 21:38:29 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我做了这个代码:

lst1 = tuple(range(1, 13))
table1 = ""
x = 0
while x < len(lst1):
    for y in range(0, 3):
        table1 += str(lst1[x]) + "\t"
        x += 1
    table1 += "\n"
print(table1)

#Output in console is:
1 2 3
4 5 6
7 8 9
10 11 12

我想再做两个表来显示其他的随机数,比如说:从0到48,但仍然只有12个从这个范围的数字会以这种格式输出。我对python相当陌生,似乎无法通过random模块理解它

这是随机数列表之一:

lst3 = tuple(range(0, 48))
table3 = ""
x = 0
while x < len(lst3):
    for y in range(0, 3):
        table3 += str(lst3[x]) + "\t"
        x += 1
    table3 += "\n"
print(random.sample(lst3, 12))

#Output is: (so basically just 12 random numbers from 1 to 47 that don't repeat)
[28, 15, 35, 11, 30, 20, 38, 3, 31, 42, 9, 24]

Tags: inforoutputlenisrangerandomprint
1条回答
网友
1楼 · 发布于 2024-04-28 21:38:29

据我所知,你想要这样的东西:

import random

lst1 = tuple(range(1, 13))
lst2 = random.sample(range(0,48), 12) # increase this 12 as per your requirements
table1 = ""
table2 = ""
x = 0
while x < len(lst1):
    for y in range(0, 3):
        table1 += str(lst1[x]) + "\t"
        x += 1
    table1 += "\n"
x = 0
while x < len(lst2):
    for y in range(0, 3):
        table2 += str(lst2[x]) + "\t"
        x += 1
    table2 += "\n"

print(table1)
print (table2)

相关问题 更多 >