Home > front end >  how to work with dict() when using depth first search tree
how to work with dict() when using depth first search tree

Time:11-05

Given:

class NoName:
  def __init__(self):
     self.some_dict = {}

  def add_new_node(self, label):
    self.some_dict[label] = {}

Consider I call the class how would I use the add_new_node method ? what prams will I need to pass in the add_new_node method ?

NoName.add_new_node('a':1)

obviously, this will not work. I need to understand how to make such positional arguments work.

CodePudding user response:

I would imagine two values that can be used as a key and that keys value. https://www.geeksforgeeks.org/python-passing-dictionary-as-arguments-to-function/#:~:text=Passing Dictionary as kwargs,*” to the passing type.

CodePudding user response:

It's this what you are looking for?

class NoName:
  def __init__(self):
     self.some_dict = {}

  def add_new_pair_key_value(self, node, value):
    self.some_dict[node] = value

  def add_new_whole_node(self, whole_node):
    self.some_dict.update(whole_node)

noname = NoName()
noname.add_new_pair_key_value('new_node', 'new_value')
print(noname.some_dict)

noname.add_new_whole_node({'label': {'new_whole_key': 'new_node_value'}})
print(noname.some_dict)

output first print:

{'new_node': 'new_value'}

output second print:

{'new_node': 'new_value', 'label': {'new_whole_key': 'new_node_value'}}
  • Related