Dat*_*'oh 3 python arrays dictionary 2d numpy
我想从表示为2d数组的对称矩阵的下三角形创建一个字典.例如,如果numpy数组是;
array([[0, 2, 3],
[2, 0, 4],
[3, 4, 0]])
Run Code Online (Sandbox Code Playgroud)
然后我希望字典看起来像;
{('1', '0'): 2, ('2', '0'): 3, ('2', '1'): 4}
Run Code Online (Sandbox Code Playgroud)
矢量有类似的帖子;
我对python比较新,所以任何帮助/建议都赞赏.
>>> arr =[[0, 2, 3],
[2, 0, 4],
[3, 4, 0]]
>>> dict(((j,i), arr[i][j]) for i in range(len(arr)) for j in range(len(arr[0])) if i<j)
{(2, 0): 3, (1, 0): 2, (2, 1): 4}
Run Code Online (Sandbox Code Playgroud)