Search code examples
networkxedges

Why networkx list(g.edges) does not return int?


pair = list(g.edges()) print(pair)

Why is the result of the second node is not an 'int'? result

I used firstnode = pair[a][0], secondnode = int(pair[a][1] for converting the number of the second node from a float to an int. But I am still confused why it is float?


Solution

  • So I am not entirely sure how your code looks like, but since you have for all of the edges in your graph a float as the second number and you want to type it as an integer, I would suggest you to do this:

    My code example:

    import networkx as nx
    
    # Create dummy graph
    g = nx.Graph()
    g.add_edge(1,2.0)
    g.add_edge(5,4.3)
    g.add_edge(8,3.9)
    
    # Get list of all edges
    pair = list(g.edges())
    print(pair)
    
    # Iterate through all edges
    for a in range(len(g.edges())):
      # Get first node of edge
      firstnode = pair[a][0]
      # Get second node of edge and type cast it to int
      secondnode = int(pair[a][1])
      # Print nodes / or execute something else here
      print(firstnode,secondnode)
      print()
    

    And this is the output:

    [(1, 2.0), (5, 4.3), (8, 3.9)]
    1 2
    
    5 4
    
    8 3
    

    I hope that helps!