Code:
tmpdict = {}
# read in.txt to dict
with open("in.txt") as f:
for line in f:
line = line.strip()
l = line.split(";")
if l[0] not in tmpdict:
tmpdict[l[0]] = {}
tmpdict[l[0]][l[1]] = l[2]
# search one outer entry which is missing
# and add it
# you have to stop iteration because you change the iterating object
def search():
for k_o in tmpdict:
for k_i in tmpdict[k_o]:
if k_i not in tmpdict:
tmpdict[k_i] = {}
tmpdict[k_i][k_o] = tmpdict[k_o][k_i]
return False
# genau genommen fehlt hier noch ein fall aber das ist vielleicht
# eine weitere übungsaufgabe ;)
# und wird von den aktuellen eingangsdaten nicht aufgedeckt
return True
# search as long as there are changes made
while search() == False:
pass
# ordered output to out.txt
with open("out.txt", "w") as f:
x = tmpdict.keys()
x.sort()
for k_o in x:
y = tmpdict[k_o].keys()
y.sort()
for k_i in y:
f.write("{};{};{}\n".format(k_o, k_i, tmpdict[k_o][k_i]))
das ist mal eine python variante. bestimmt nicht ideal gelöst aber das ergebnis ist nicht das schlechteste.
Lesezeichen