python:organising a text file of users scores into a list -
i have text file containing this:
john = 10 lucy = 2 tom = 4, hardy = 7 tom = 5 tom = 3 hardy = 7 christoper = 4 kim = 9 tom = 4 john = 5 john = 7 john = 6
i need read file , organize list using last 3 scores of users. have been trying hours cant working, please thankyou
this might help:
dic={} open("demo.txt","r") f: line in f: a,b=line.strip().replace(" ","").split("=") if a.lower() in dic: dic[a.lower()].append(b) else: dic[a.lower()]=[] dic[a.lower()].append(b) d in dic: dic[d]= dic[d][-3:] print dic
output:
{'christoper': ['4'], 'lucy': ['2'], 'hardy': ['7', '7'], 'kim': ['9'], 'tom': ['5', '3', '4'], 'john': ['5', '7', '6']}
Comments
Post a Comment