Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用随机列表生成二元矩阵_Python_List_Numpy_Loops_Dictionary - Fatal编程技术网

Python 用随机列表生成二元矩阵

Python 用随机列表生成二元矩阵,python,list,numpy,loops,dictionary,Python,List,Numpy,Loops,Dictionary,首先是我的代码,我有一些问题,我觉得答案很简单,但我太盲目了,看不见 dic = {} for i in range(0,9): dic['rand_num{0}'.format(i)] = np.sort(random.sample(range(0,8),5)) mat = np.zeros([8,8]) for rand_num in dic.values(): print(rand_num) for i in range(0,5): matr

首先是我的代码,我有一些问题,我觉得答案很简单,但我太盲目了,看不见

dic = {}

for i in range(0,9):
    dic['rand_num{0}'.format(i)] = np.sort(random.sample(range(0,8),5))

mat = np.zeros([8,8])


for rand_num in dic.values():
    print(rand_num)
    for i in range(0,5):
        matrixval = rand_num[i]
        #print(matrixval)
        for j in range(0,8):
            mat[matrixval,j] = 1


print(mat)


我创建了8个不同的列表,这将帮助我确定在矩阵中设置1的位置

例如,如果我的第一个列表如下所示:

随机列表=[0,3,5,6,7]

第一行或第一列的矩阵应如下所示

矩阵=[1,0,0,1,0,1,1,1,1]

矩阵值的位置在第二个循环中设置:
用于范围(0,5)中的i:matrixval=rand_num[i]
。然而,当我运行代码时,我的矩阵只给出1,而不是1和0。我觉得我在上一个for循环中遇到了一个迭代问题,我不知道如何解决它


如果有人能帮助我,或者只是给我一个提示,如何真正解决我这个希望渺茫的错误,我会非常感激

希望这能提供预期的结果:

dic = {}

for i in range(0,8):
    dic['rand_num{0}'.format(i)] = np.sort(random.sample(range(0,8),5))

mat = np.zeros([8,8])

for j, rand_num in enumerate(dic.values()):
    print(rand_num)
    for i in range(0,5):
        matrixval = rand_num[i]
        mat[j,matrixval] = 1

print(mat)
输出:

[1 2 3 5 7]
[0 1 2 5 6]
[0 1 2 3 5]
[1 2 3 4 6]
[0 1 3 4 7]
[1 2 4 5 7]
[0 1 3 4 7]
[1 2 3 4 6]
[[0. 1. 1. 1. 0. 1. 0. 1.]
 [1. 1. 1. 0. 0. 1. 1. 0.]
 [1. 1. 1. 1. 0. 1. 0. 0.]
 [0. 1. 1. 1. 1. 0. 1. 0.]
 [1. 1. 0. 1. 1. 0. 0. 1.]
 [0. 1. 1. 0. 1. 1. 0. 1.]
 [1. 1. 0. 1. 1. 0. 0. 1.]
 [0. 1. 1. 1. 1. 0. 1. 0.]]

不需要您正在使用的
dic
。这里有一个没有它的解决方案

随机导入
列表=[]
对于范围(0,9)内的i:
lists.append(已排序(random.sample(范围(0,8,5)))
mat=[]
对于列表中的lst:
arr=[0]*8
对于lst中的索引:
arr[index]=1
材料附加(arr)
印刷品(垫子)
产出:

[[0, 1, 1, 0, 0, 1, 1, 1],
 [1, 0, 1, 1, 0, 0, 1, 1],
 [1, 1, 1, 1, 0, 0, 0, 1],
 [0, 1, 1, 1, 0, 1, 1, 0],
 [1, 1, 0, 1, 1, 0, 1, 0],
 [1, 0, 1, 1, 0, 1, 1, 0],
 [0, 1, 1, 1, 1, 1, 0, 0],
 [0, 1, 1, 0, 1, 1, 1, 0],
 [1, 0, 1, 0, 1, 0, 1, 1]]
[[1 0 1 0 1 0 1 1]
 [0 0 0 0 1 0 1 1]
 [1 0 1 1 0 0 1 1]
 [1 1 0 1 0 1 1 0]
 [0 0 1 0 0 1 1 1]
 [1 0 0 0 1 1 1 1]
 [1 1 0 1 0 0 1 1]
 [1 0 1 0 0 1 0 0]]

还可以使用生成二进制二维数组

将numpy导入为np
arr=np.random.randint(2,size=(8,8))
打印(arr)
产出:

[[0, 1, 1, 0, 0, 1, 1, 1],
 [1, 0, 1, 1, 0, 0, 1, 1],
 [1, 1, 1, 1, 0, 0, 0, 1],
 [0, 1, 1, 1, 0, 1, 1, 0],
 [1, 1, 0, 1, 1, 0, 1, 0],
 [1, 0, 1, 1, 0, 1, 1, 0],
 [0, 1, 1, 1, 1, 1, 0, 0],
 [0, 1, 1, 0, 1, 1, 1, 0],
 [1, 0, 1, 0, 1, 0, 1, 1]]
[[1 0 1 0 1 0 1 1]
 [0 0 0 0 1 0 1 1]
 [1 0 1 1 0 0 1 1]
 [1 1 0 1 0 1 1 0]
 [0 0 1 0 0 1 1 1]
 [1 0 0 0 1 1 1 1]
 [1 1 0 1 0 0 1 1]
 [1 0 1 0 0 1 0 0]]

matrix=[int(rand_列表中的x)表示范围(9)]
。对于完全随机的二进制矩阵,您可以尝试:
np.random.randint(0,2,8*8)。重塑(8,8)
@pault Good idea+1--将提到,
randint
有一个使
重塑
过时的
大小
参数。看看我下面的答案。谢谢,我没有想过,肯定是另一种解决方法!有时最好不要重新发明轮子。