Skip to content

Inconsistent Node ID Types Cause KeyError When Using Integer Node IDs #313

@hackl

Description

@hackl

When constructing a graph from an edge list with integer node IDs, iterating over n.nodes and accessing dictionary-based methods such as degrees() results in a KeyError.

This is not working:

n = pp.Graph.from_edge_list([(0, 1), (1, 2), (2, 1)])
for v in n.nodes:
    print(n.degrees()[v])

Error:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[25], line 3
      1 n = pp.Graph.from_edge_list([(0, 1), (1, 2), (2, 1)])
      2 for v in n.nodes:
----> 3     print(n.degrees()[v])

KeyError: 0

Working example with string node IDs

n = pp.Graph.from_edge_list([('A', 'B'), ('B', 'C'), ('C', 'A')])
for v in n.nodes:
    print(n.degrees()[v]) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions