随机数格子 4 x 6 Python 3
Random number grid 4 x 6 Python 3
我是 Python 3 的新手,想生成一个随机网格 4x6 数字。
我尝试了各种方法,但最终还是在新行上打印,或者在每行上打印 4x6 相同的数字等。
一些代码示例(我尝试了很多不同的方法,但我必须重新输入它们,因为我使用的是 linux 到 运行 Python 的虚拟框:
import random
def grid_maker(h,v):
for y in range(1, 5):
x = random.randint(1, 100)
grid = [[str(x) for _ in range(v)] for _ in range (h)]
grid[0][0] = "o"
return grid
print ('\n'.join(''.join(row) for row in grid_maker(6,5)))
输出
o85858585
8585858585
8585858585
8585858585
8585858585
8585858585
期望的输出
82 2 30 9
45 65 54 14
23 4 32 42
8 84 40 80
43 23 5 62
23 43 3 5
另一个例子
for t in range(1, 5)
i = random.randint(1, 100)
grid = [[i], [i], [i], [i]]
from pprint import pprint
pprint(grid)
输出
[[100], [100], [100], [100]]
[[1], [1], [1], [1]]
[[80], [80], [80], [80]]
[[12], [12], [12], [12]]
期望的输出
[[34], [12], [2], [15]]
[[64], [34], [53], [4]]
[[35], [34], [61], [33]]
[[34], [55], [64], [23]]
另一个例子
ar = []
for t in range(1, 5):
i = random.randint(1, 100)
ar.append(i)
for t in range(1, 7):
for i in range(1, 5):
print(ar[t-1], end='')
但这会打印出 585858585555555559595959343434342222222288888888root@root:/home/user#
谢谢
你每次在循环中只调用一次随机所以保持使用相同的值,在列表中调用它 comp:
def grid_maker(h,v):
for y in range(1, 5):
# add call to list comp to get a new number each iteration
grid = [[str(random.randint(1,100)) for _ in range(v)] for _ in range (h)]
grid[0][0] = "o"
return grid
print ('\n'.join(' '.join(row) for row in grid_maker(6,4))
o 23 47 30
25 21 32 28
26 76 71 14
54 15 94 77
50 50 29 62
59 92 60 57
如果您不想要 "o"
则不要添加它,只需删除 grid[0][0] = "o"
第一个循环也是多余的你不断创建新列表但只返回一个:
def grid_maker(h, v):
return [[str(random.randint(1, 100)) for _ in range(v)] for _ in range(h)]
我们也可以只在函数中完成:
def grid_maker(h, v):
return '\n'.join(' '.join(row) for row in [[str(random.randint(1, 100)) for _ in range(v)] for _ in range(h)])
print(grid_maker(6, 4)) # 6,4 not 6,5
26 96 50 46
4 57 50 80
36 24 23 66
71 33 44 15
54 15 77 54
52 99 60 99
要打印适用相同逻辑的列表列表,您需要每次调用 randint:
for t in range(6):
print([[random.randint(1,100)], [random.randint(1,100)], [random.randint(1,100)], [random.randint(1,100)]])
[[90], [57], [21], [11]]
[[84], [80], [100], [47]]
[[25], [37], [87], [97]]
[[78], [90], [100], [74]]
[[96], [83], [59], [2]]
[[83], [19], [90], [4]]
您在第一个例子中只生成了一个随机数:
import random
def grid_maker(h, v):
return [
[str(random.randint(1, 100)) for _ in range(v)]
for _ in range(h)
]
print ('\n'.join(' '.join(row) for row in grid_maker(6, 5)))
第二个:
import random
ar = [
[[random.randint(1, 100)] for t in range(1, 5)]
for i in range(1, 5)
]
for row in ar:
print(row)
我是 Python 3 的新手,想生成一个随机网格 4x6 数字。 我尝试了各种方法,但最终还是在新行上打印,或者在每行上打印 4x6 相同的数字等。
一些代码示例(我尝试了很多不同的方法,但我必须重新输入它们,因为我使用的是 linux 到 运行 Python 的虚拟框:
import random
def grid_maker(h,v):
for y in range(1, 5):
x = random.randint(1, 100)
grid = [[str(x) for _ in range(v)] for _ in range (h)]
grid[0][0] = "o"
return grid
print ('\n'.join(''.join(row) for row in grid_maker(6,5)))
输出
o85858585
8585858585
8585858585
8585858585
8585858585
8585858585
期望的输出
82 2 30 9
45 65 54 14
23 4 32 42
8 84 40 80
43 23 5 62
23 43 3 5
另一个例子
for t in range(1, 5)
i = random.randint(1, 100)
grid = [[i], [i], [i], [i]]
from pprint import pprint
pprint(grid)
输出
[[100], [100], [100], [100]]
[[1], [1], [1], [1]]
[[80], [80], [80], [80]]
[[12], [12], [12], [12]]
期望的输出
[[34], [12], [2], [15]]
[[64], [34], [53], [4]]
[[35], [34], [61], [33]]
[[34], [55], [64], [23]]
另一个例子
ar = []
for t in range(1, 5):
i = random.randint(1, 100)
ar.append(i)
for t in range(1, 7):
for i in range(1, 5):
print(ar[t-1], end='')
但这会打印出 585858585555555559595959343434342222222288888888root@root:/home/user#
谢谢
你每次在循环中只调用一次随机所以保持使用相同的值,在列表中调用它 comp:
def grid_maker(h,v):
for y in range(1, 5):
# add call to list comp to get a new number each iteration
grid = [[str(random.randint(1,100)) for _ in range(v)] for _ in range (h)]
grid[0][0] = "o"
return grid
print ('\n'.join(' '.join(row) for row in grid_maker(6,4))
o 23 47 30
25 21 32 28
26 76 71 14
54 15 94 77
50 50 29 62
59 92 60 57
如果您不想要 "o"
则不要添加它,只需删除 grid[0][0] = "o"
第一个循环也是多余的你不断创建新列表但只返回一个:
def grid_maker(h, v):
return [[str(random.randint(1, 100)) for _ in range(v)] for _ in range(h)]
我们也可以只在函数中完成:
def grid_maker(h, v):
return '\n'.join(' '.join(row) for row in [[str(random.randint(1, 100)) for _ in range(v)] for _ in range(h)])
print(grid_maker(6, 4)) # 6,4 not 6,5
26 96 50 46
4 57 50 80
36 24 23 66
71 33 44 15
54 15 77 54
52 99 60 99
要打印适用相同逻辑的列表列表,您需要每次调用 randint:
for t in range(6):
print([[random.randint(1,100)], [random.randint(1,100)], [random.randint(1,100)], [random.randint(1,100)]])
[[90], [57], [21], [11]]
[[84], [80], [100], [47]]
[[25], [37], [87], [97]]
[[78], [90], [100], [74]]
[[96], [83], [59], [2]]
[[83], [19], [90], [4]]
您在第一个例子中只生成了一个随机数:
import random
def grid_maker(h, v):
return [
[str(random.randint(1, 100)) for _ in range(v)]
for _ in range(h)
]
print ('\n'.join(' '.join(row) for row in grid_maker(6, 5)))
第二个:
import random
ar = [
[[random.randint(1, 100)] for t in range(1, 5)]
for i in range(1, 5)
]
for row in ar:
print(row)