Skip to content

Instantly share code, notes, and snippets.

@alaiacano
Created August 15, 2013 20:38
Show Gist options
  • Save alaiacano/6244611 to your computer and use it in GitHub Desktop.
Save alaiacano/6244611 to your computer and use it in GitHub Desktop.
# input data:
# a,1
# b,2
# a,3
# a,4
# a,7
# b,4
# b,0
# $ cat data.csv | python stat.py
# a {'count': 4, 'sum': 15.0, 'avg': 3.75}
# b {'count': 3, 'sum': 6.0, 'avg': 2.0}
import sys
import collections
class Stat(object):
def __init__(self):
self.sum = 0.0
self.count = 0
def update(self, value):
self.count += 1
self.sum += value
def status(self):
return {'sum': self.sum, 'count': self.count, 'avg': self.sum / self.count}
def stat_factory():
return Stat()
objects = collections.defaultdict(stat_factory)
for line in sys.stdin:
key, val = line.split(",")
objects[key].update(float(val))
for k, v in objects.items():
print k, v.status()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment