将邻接矩阵转换为邻接表

Transforming adjaceny-matrix into an adjaceny-list

对于下面给定的邻接矩阵: [[0, 1, 0, 4], [1, 0, 3, 1], [0, 3, 0, 2], [4, 1, 2, 0]]

我如何将它转换成如下所示的邻接列表,其中每个元组是 (node,weight)

[[(1, 1), (3, 4)], [(0, 1), (2, 3), (3, 1)], [(1, 3), (3, 2)], [(0, 4), (1, 1), (2, 2)]]

非常感谢!

使用双重理解:

a=[[0, 1, 0, 4], [1, 0, 3, 1], [0, 3, 0, 2], [4, 1, 2, 0]]
print([[(i,x) for i,x in enumerate(y) if x!=0] for y in a])