Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data = [('Cape Town - Durban','RT',95),
- ('Cape Town - Durban','RT',95),
- ('Cape Town - Durban','BT',185),
- ('Cape Town - Durban','BE',285),
- ('Cape Town - Durban','RT',100),
- ('Cape Town - Durban','BT',190),
- ('Cape Town - Durban','BE',290),]
- delay={}
- # naive implementation
- for path,cos,ms in data:
- if not cos in delay:
- delay[cos] = {path: {ms:1}}
- elif not path in delay[cos]:
- delay[cos][path] = {ms:1}
- elif not ms in delay[cos][path]:
- delay[cos][path][ms] = 1
- else:
- delay[cos][path][ms]+= 1
- delay={}
- # setdefault implementation
- for path,cos,ms in data:
- delay.setdefault(cos, {}).setdefault(path, {}).setdefault(ms,0)
- delay[cos][path][ms]+= 1
- for cos in delay.keys():
- for path,values in delay[cos].items():
- histogram=[]
- # looping around the test data range for demonstration purposes
- for i in range(90,300):
- # using setdefault has the side-effect of padding the delay dict for non-existent samples
- histogram.append(str(delay[cos][path].setdefault(i, 0)))
- print '%s: %s: %s' % (cos,path,' '.join(histogram))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement