Advertisement
Guest User

Untitled

a guest
May 6th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. d = {Apple: [[Category1, 14], [Category2, 12], [Category2, 8]], Orange : [[Category2, 12], [Category2, 12], [Category3, 2]]}
  2.  
  3. d = {Apple: [[Category1, 14], [Category2, 20]], Orange: [[Category2, 24], [Category3, 2]]}
  4.  
  5. output = defaultdict(set)
  6. for key, value in d.items():
  7. for item in value:
  8. total += int(value[1])
  9. output[key].add(value[0],total)
  10. total = 0
  11.  
  12. def tally_up(dct):
  13. totals = dict() # Maps keys to dicts of totals
  14. for key, tuples in dct.items():
  15. subtotals = collections.defaultdict(lambda: 0) # Maps keys to counts
  16. for subkey, count in tuples:
  17. subtotals[subkey] += count
  18. totals[key] = dict(subtotals)
  19. return totals
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement