graph - Is there a Python function for this piece of code? -


i created graph putting nodes in list. node class attributes 'parent', 'daughters', , 'edge'. want check if symbol in edges of of daughter-nodes of current node, , if so, change current node node. if not, create new node edge, , change current node it.

i this:

match = false daughter in currentnode.daughters:     if daughter.edge == currentsymbol:         match = true         currentnode = daughter if match == false:     trie.append(node(currentnode, [], currentsymbol)     currentnode = trie[-1] 

the use of 'match' seems inelegant me. there better syntax checking if edge exists among daughters, , if so, updating current node daughter, , if not, creating node?

you can use for/else here:

for daughter in currentnode.daughters:     if daughter.edge == currentsymbol:         currentnode = daughter         break else:     trie.append(node(currentnode, [], currentsymbol)     currentnode = trie[-1] 

the else case executed if for loop exited normally, i.e. without break


Comments