class Solm: def __init__(self, sona): self.sona=sona self.alamad={} self.kogus=0 def lisaSona(self, sona, kogus): if not sona in self.alamad.keys(): self.alamad[sona]=Solm(sona) self.alamad[sona].kogus+=kogus return self.alamad[sona] def tryki(self, kaugus): if self.kogus<10: return print "\t"*kaugus, self.sona, self.kogus solmed=self.alamad.values() solmed.sort(key=lambda x: x.kogus, reverse=True) # self.alamad[sona].tryki(kaugus+1) for solm in solmed: solm.tryki(kaugus+1) def trykiXML(self, kaugus): print " "*kaugus, "" solmed=self.alamad.values() solmed.sort(key=lambda x: x.kogus, reverse=True) for solm in solmed: solm.trykiXML(kaugus+1) print " "*kaugus, "" def loeFail(algsolm): # f=open("vastavused2.txt", "r") f=open("oppijavastavused2.txt", "r") r=f.readlines() f.close() for rida in r: rida=rida.strip() if len(rida)<2: continue solm=algsolm m=rida.split(",") k=int(m[0].strip()) algsolm.kogus+=k m=m[1:] for sona in m: if len(sona.strip())>1: solm=solm.lisaSona(sona.strip(), k) def algus(): s=Solm("'juur'") loeFail(s) s.tryki(0) # s.trykiXML(0) # for solm in s.alamad.values(): # solm.tryki(0) algus()