Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Removing specific nodes and adjusting node number in a graph in Python

I have a list of nodes. I want to remove T from nodes and update the node number. The expected output is presented.

nodes={(0, 0): 1,
 (0, 1): 2,
 (0, 2): 3,
 (1, 0): 4,
 (1, 1): 5,
 (1, 2): 6,
 (2, 0): 7,
 (2, 1): 8,
 (2, 2): 9}

T=[(2, 0),(2,1)]

The expected output is

newnodes={(0, 0): 1,
 (0, 1): 2,
 (0, 2): 3,
 (1, 0): 4,
 (1, 1): 5,
 (1, 2): 6,
 (2, 2): 7}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

nodes={(0, 0): 1,
 (0, 1): 2,
 (0, 2): 3,
 (1, 0): 4,
 (1, 1): 5,
 (1, 2): 6,
 (2, 0): 7,
 (2, 1): 8,
 (2, 2): 9}

T=[(2, 0),(2,1)]

# For every node inside T
for t in T:

   # If the node exists inside nodes
   if (t in nodes):

      # Get the current index from the node
      index = nodes[t]

      # And remove the node from the dictionary
      nodes.pop(t)

      # Now, we update the indexes from nodes that were inserted after
      # node t
      for key in nodes:
         if (nodes[key] > index):
            nodes[key] -= 1

# Done
print(nodes)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading