我想创建一个随机矩阵,如[[[100,50,25],[22,75,195]]]
我的代码是
n = 1
r = 2
e = 3
sup = []
for i in range(n):
sup1 = []
for c in range(r):
sup0 = list (random.randint (200, 0, e))
sup1.append (sup0)
sup.append (sup1)
print sup
但python给我错误。
答案 0 :(得分:2)
这应该有用(不知道e
做了什么):
sup = [[random.randint(0, 200) for _ in range(r)] for _ in range(n)]
答案 1 :(得分:0)
您可以使用numpy
直接获取所需大小的随机矩阵,并使用给定范围内的值。
>>> numpy.random.randint(low = 0, high = 200, size=(2, 4))
array([[ 75, 21, 132, 90],
[112, 11, 104, 114]])
>>> r = 2
>>> n = 1
>>> numpy.random.randint(low = 0, high = 200, size=(r, n))
array([[94],
[51]])