我编写了一个代码来随机填充矩阵0或1。但以4-5行结尾。我要把它排在一排。
pop_size = (8,10)
initial_pop = np.empty((pop_size))
for i in range(pop_size[0]):
for j in range(pop_size[1]):
initial_pop[i][j] = rd.randint(0,1)发布于 2019-01-07 18:15:14
使用numpy的randint方法
matrix = np.random.randint(2, size=pop_size)发布于 2019-01-07 18:11:32
我知道你在使用NumPy。如果是的话,答案是:
np.random.randint(2, size=pop_size)下面是关于这个例程的NumPy docs文章:链接。Numpy有很好的文档记录,下次试着自己检查文档。
编辑:参数应该是2,而不是1。
发布于 2019-01-07 18:14:54
在标准Python中,请尝试:
from random import randint
x = [[randint(0,1) for _ in range(8)] for _ in range(10)]https://stackoverflow.com/questions/54079524
复制相似问题