python - Anyone good at nested loops? -
first post here fyi! got graph looks this:
graphs = {'g1': [ [ ['a1', 0], ['a2', 0] ], [ ['e1', 0] ], {'a1': ['e1'], 'a2': ['e1'] } ] }
what graph representing abstract data structure of graph nodes , edges basically. can represent 1 or several graphs.
i'm trying "reach" "a1" can write file example below:
a1,0;a2,0;a3,0
e1,0;e2,0
a1,e1,e2;a2,e1;a3,2
the intergers thing has 2 states 1 or 0 depending if have crossed or not crossed node or edge.
in order write file need nested loop reaches each node (vertex) , edge in graph.
i have yet not found tutorials nested loops items in lists. talk for in range(3):
etc.
this code far:
def find_vertex(graphs): key in graphs: print(str(graphs[key][0][0][0]))
this prints out a1 but... yeah if want should use nested for-loop right?
i don't know how works. link of explained appreciated lot since don't seem grip this.
my solution, took time.
def save_graph(graphs): string = ""
tname = input("skriv in namnet på filens namn: ") thand = open(tname, 'w') key in graphs: objekt in graphs[key][0]: string += str(objekt[0]) + "," string += str(objekt[1]) + "," string += ";" string += "\n" #print(string) key in graphs: objekt in graphs[key][1]: string += str(objekt[0]) + "," string += str(objekt[1]) + "," string += ";" string += "\n" graf in graphs: key, value in graphs[graf][2].items(): string += (key) + "," + (value[0]) + ";" string = string.replace(",;", "") string = string[:-1] print(string) thand.write(str(string)) thand.close()
Comments
Post a Comment