Skip to content

Commit

Permalink
counting preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
mwartell committed Mar 24, 2016
1 parent 3f70c9a commit 1b11329
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
16 changes: 16 additions & 0 deletions count1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import csv
from collections import defaultdict

def ice_cream_counts(path):
"""Returns gender/preference counts from csv in path."""
counts = {'m': defaultdict(int), 'f': defaultdict(int)}

with open(path) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
counts[row['gender']][row['preference']] += 1

return counts

counts = ice_cream_counts('ice-cream.csv')
print(counts)
16 changes: 16 additions & 0 deletions count2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import csv
from collections import defaultdict

def ice_cream_counts(path):
"""Returns gender/preference counts from csv in path."""
counts = {'m': defaultdict(int), 'f': defaultdict(int)}

with open(path) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
counts[row['gender']][row['preference']] += 1

return counts

counts = ice_cream_counts('ice-cream.csv')
print(counts)
2 changes: 2 additions & 0 deletions read3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ def read_data(path):
with open(path) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
assert row['gender'] in 'mf'
assert row['preference'] in 'ps'
print(row['gender'], row['preference'])

read_data('ice-cream.csv')

0 comments on commit 1b11329

Please sign in to comment.