我在文本文档中有一个48乘36的零矩阵,我想将随机值替换为1,然后保存它。这是我的代码:
import numpy as np
import random
a = np.zeros((48,36), dtype=np.int)
x = random.randrange(48)
y = random.randrange(36)
a[x,y] = 1
f = open('Earth_Village2', 'w')
f.write(str(a))
f.close()
print('done!')
但文本文件显示了这一点:
[[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
...,
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]
[0 0 0 ..., 0 0 0]]
当我有一个较小的矩阵(10 x 10)时,它确实有效:
(x = 7和y = 7)
[[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0]]
如何显示整个48乘36矩阵?