from collections import defaultdict loendur=defaultdict(lambda: 0) for lause in open("lemmadpikk.txt", encoding="utf-8"): for sona in lause.split(): loendur[sona]+=1 votmed=list(loendur.keys()) votmed.sort(key=lambda voti: -loendur[voti]) f2=open("sonakogused.txt", "w", encoding="utf-8") for voti in votmed: print(voti, loendur[voti], sep=" ", file=f2) f2.close()