如何使用顺序键将Numpy数组转换为Python字典?

Fai*_*rbw 5 python arrays dictionary numpy

我有一个像这样的numpy数组形式的矩阵:

myarray = np.array[[0,400,405,411,415,417,418,0]
                   [0,404,412,419,423,422,422,0]
                   [0,409,416,421,424,425,425,0]
                   [0,411,414,417,420,423,426,0]
                   [0,409,410,410,413,419,424,0]
                   [0,405,404,404,409,414,419,0]]
Run Code Online (Sandbox Code Playgroud)

还有空字典:

dict = { }
Run Code Online (Sandbox Code Playgroud)

在我的情况下,我想将该数组转换为python字典,其中字典的键是从左上角值(myarray[0][0])计算的序号,直到myarray[5][7]按行交错的右下角值().所以结果将是这样的:

dict = { 1 : 0, 2 : 400, 3: 405, ........, 47 : 419 ,48 : 0 } 
Run Code Online (Sandbox Code Playgroud)

有这种情况的解决方案吗?希望得到你的帮助..任何帮助将非常感谢..

Jun*_*sor 14

使用flatten然后在enumerate1开头的帮助下创建字典:

myarray = np.array([[0,400,405,411,415,417,418,0],
                   [0,404,412,419,423,422,422,0],
                   [0,409,416,421,424,425,425,0],
                   [0,411,414,417,420,423,426,0],
                   [0,409,410,410,413,419,424,0],
                   [0,405,404,404,409,414,419,0]])

d = dict(enumerate(myarray.flatten(), 1))
Run Code Online (Sandbox Code Playgroud)

产量d:

{1: 0,
 2: 400,
 3: 405,
 4: 411,
 5: 415,
 6: 417,
 7: 418,
 8: 0,
 9: 0,
 10: 404,
 ...
Run Code Online (Sandbox Code Playgroud)