Skip to content

Instantly share code, notes, and snippets.

@imosquera
Last active August 29, 2015 14:03
Show Gist options
  • Save imosquera/584dc0ba7facc6b482cb to your computer and use it in GitHub Desktop.
Save imosquera/584dc0ba7facc6b482cb to your computer and use it in GitHub Desktop.
events = [{'v': 331, 'c': 'USA'}, {'v': 432, 'c': 'MX'},{'v':237 ,'c':'MX'}, {'v': 239, 'c': 'USA'}]
sorted_events = sorted(events, key=lambda e: e['c'])
for key, groups in groupby(sorted_events, lambda e: e['c']):
sum_events = sum([g['v'] for g in groups])
print "country: %s has %i total events" % (key, sum_events)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment