<div dir="ltr"><div>Hello Everyone <br><br></div>I am very much new to python scripting and i am here for knowledge exchange. <br><br><pre class=""><div class="" id="file-nodeneighbhors-LC1">I am trying to read a file with node pairs and weight. <br>
I have to find the neighbours of each pair individual and combined both also count them. <br>Later find the ratio of the neighbours that each node has. I am stuck with finding nodes. </div><div class="" id="file-nodeneighbhors-LC2">
 </div><div class="" id="file-nodeneighbhors-LC3">infile.txt</div><div class="" id="file-nodeneighbhors-LC4"> </div><div class="" id="file-nodeneighbhors-LC5">0_node1 0_node2 0w</div><div class="" id="file-nodeneighbhors-LC6">
1_node1 1_node2 1w</div><div class="" id="file-nodeneighbhors-LC7">2_node1 2_node2 2w</div><div class="" id="file-nodeneighbhors-LC8">3_node1 3_node2 3w</div><div class="" id="file-nodeneighbhors-LC9">4_node1 4_node2 4w</div>
<div class="" id="file-nodeneighbhors-LC10"> </div><div class="" id="file-nodeneighbhors-LC11"> </div><div class="" id="file-nodeneighbhors-LC12">import networkx as nx</div><div class="" id="file-nodeneighbhors-LC13">import matplotlib.pyplot as plt</div>
<div class="" id="file-nodeneighbhors-LC14"> </div><div class="" id="file-nodeneighbhors-LC15">G=nx.Graph()</div><div class="" id="file-nodeneighbhors-LC16">G = nx.read_edgelist('infile.txt', data=[("weight", float)])</div>
<div class="" id="file-nodeneighbhors-LC17"> </div><div class="" id="file-nodeneighbhors-LC18">def get_triangle(G):</div><div class="" id="file-nodeneighbhors-LC19">  for n1 in G.nodes:</div><div class="" id="file-nodeneighbhors-LC20">
    neighbors1 = set(G[n1])</div><div class="" id="file-nodeneighbhors-LC21">    for n2 in filter(lambda x: x>n1, nodes):</div><div class="" id="file-nodeneighbhors-LC22">  neighbors2 = set(G[n2])</div><div class="" id="file-nodeneighbhors-LC23">
        common = neighbors1 & neighbors2</div><div class="" id="file-nodeneighbhors-LC24">  for n3 in filter(lambda x: x>n2, common):</div><div class="" id="file-nodeneighbhors-LC25">    print n1</div><div class="" id="file-nodeneighbhors-LC26">
          print n2</div><div class="" id="file-nodeneighbhors-LC27">      print n3<br><br></div><div class="" id="file-nodeneighbhors-LC27">Any suggestion will be appreciated. Thanks <br></div></pre><br></div>