Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. data = [('Cape Town - Durban','RT',95),
  2.         ('Cape Town - Durban','RT',95),
  3.         ('Cape Town - Durban','BT',185),
  4.         ('Cape Town - Durban','BE',285),
  5.         ('Cape Town - Durban','RT',100),
  6.         ('Cape Town - Durban','BT',190),
  7.         ('Cape Town - Durban','BE',290),]
  8. delay={}
  9. # naive implementation
  10. for path,cos,ms in data:
  11.     if not cos in delay:
  12.         delay[cos] = {path: {ms:1}}
  13.     elif not path in delay[cos]:
  14.         delay[cos][path] = {ms:1}
  15.     elif not ms in delay[cos][path]:
  16.         delay[cos][path][ms] = 1
  17.     else:
  18.         delay[cos][path][ms]+= 1
  19.  
  20. delay={}
  21. # setdefault implementation
  22. for path,cos,ms in data:
  23.     delay.setdefault(cos, {}).setdefault(path, {}).setdefault(ms,0)
  24.     delay[cos][path][ms]+= 1
  25.  
  26. for cos in delay.keys():
  27.     for path,values in delay[cos].items():
  28.         histogram=[]
  29.         # looping around the test data range for demonstration purposes
  30.         for i in range(90,300):
  31.             # using setdefault has the side-effect of padding the delay dict for non-existent samples
  32.             histogram.append(str(delay[cos][path].setdefault(i, 0)))
  33.         print '%s: %s: %s' % (cos,path,' '.join(histogram))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement