PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 7, 2022

[FIXED] How to modify single object inside dict values stored as a set?

 November 07, 2022     dictionary, python, python-3.x, set     No comments   

Issue

I have a dictionary which represents graph. Key is Node class and values are set of Nodes. So it has this structure: dict[Node] = {Node, Node, Node}

class Node:

def __init__(self, row, column, region):
    self.id = f'{str(row)}-{str(column)}'
    self.region = region
    self.visited = False

In code below I need to update visited property of Node class.

        while nodes_queue:
        current_node = nodes_queue.pop()

        for edge in self.map[current_node]: 
            if edge.region == root.region and not edge.visited:
                edge.visited = True # Not updated!
                nodes_queue.append(edge)

But it looks like I get view of Node objects instead of actual objects. When I update visited property in for loop and get it from next iteration, the property is still set to False


Solution

I've figured it out. I was storing different Node objects as key and what was in values set in my dictionary. I created context of all Nodes and get Node from there by its id.

def get_node_from_context(self, row, column, region):
        node = Node(row, column, region)

        if node not in self.__graph_context__:
            self.__graph_context__[node] = node
        else:
            node = self.__graph_context__[node]

        return node


Answered By - tojzke
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing