擅长:python、mysql、java
<p>在代码中,<code>for item in (graph_matrix):</code>,这里<code>item</code>表示一个数字列表。
并且不能使用数字列表作为如下的矩阵索引:<code>graph_matrix[item][node]</code></p>
<p>据我所知,你的问题是,你想知道三个节点是否相互连接。为此,您可以通过以下方式修改代码:</p>
<pre><code>def society(graph_matrix, node):
for i in range(len(graph_matrix[node])):
for j in range(len(graph_matrix[node])):
if graph_matrix[node][i] and graph_matrix[node][j] and graph_matrix[i][j] == 1:
return True
return False
gmatrix = [ [0,1,1,1,0],
[1,0,0,1,0],
[1,0,0,0,1],
[1,1,0,0,0],
[0,0,1,0,0] ]
print(society(gmatrix, 0));
</code></pre>
<p>这里,<code>len(graph_matrix[node])</code>将返回<code>graph_matrix[node]</code>的<strong>长度</strong>,<code>range(len(graph_matrix[node]))</code>将从<strong>0</strong>迭代到<strong>length-1</strong></p>