Python 将具有关系的矩阵转换为列表

Python convert matrix with relations to list

我有一个如下所示的矩阵:

    a   b   c   d
a   0   1   0   0
b   0   0   1   1
c   1   0   0   1
d   1   0   0   0

其中 1 表示行和列之间的单向关系。 例如:

a interacts with b
b interacts with c and d
c interacts with a and d
...

我需要的是一个包含三列的列表,忽略 0 并列出交互。 例如:

a  1  b
b  1  c
b  1  d
c  1  a
...

目前我正在使用 numpy 并从 csv 文件中读取初始矩阵。

data = np.array(list(csv.reader(open("input.csv"))))

当我创建我的玩具数据矩阵 a:

a = numpy.array([[0,1],[1,0]])
coords = ('a', 'b')
for i in range(len(coords)):
    for j in range(len(coords)):
        if a[i,j]:
            print coords[i], 1, coords[j]

它输出:

a 1 b
b 1 a