'''
Created on Nov 2, 2011

@author: rodger
'''
# dictionary of schools and how many students at an event
# are from each school

d = {'duke':30, 'unc':50, 'ncsu':40}

print "d.values()", d.values()
print "d.keys()", d.keys()
d['duke'] = 80
d.update({'ecu':40, 'uncc':70})
print "d.values()", d.values()

# what is the difference between these 3?

print "d.items()", d.items()
print "d.iteritems()", d.iteritems()
print "d", d

# problem - how many schools have more than 40 attendees?
count = len([x for x in d.values() if x > 40])
print "Number of schools with more than 40 attendees is ", count

count = len(dict((k,v) for (k,v) in d.iteritems() if v>40))
print "Number of schools with more than 40 attendees is ", count

count = 0
for item in d.values():
    if item > 40:
        count += 1
print "Number of schools with more than 40 attendees is ", count      

count = 0
for item in d.keys():
    if d.get(item) > 40:
        count += 1
print "Number of schools with more than 40 attendees is ", count    


# problem - which schools have more than 40 attendees - give a list
schools = []
for item in d.keys():
    if d.get(item) > 40:
        schools.append(item)
print "Number of schools with more than 40 attendees is ", schools  

schools = []
for item in d.keys():
    if d[item] > 40:
        schools += [item]
print "Number of schools with more than 40 attendees is ", schools  

schools = []
for (item,value) in d.items():
    if value > 40:
        schools += [item]
print "Number of schools with more than 40 attendees is ", schools  

schools = []
for (item,value) in d.iteritems():
    if value > 40:
        schools += [item]
print "Number of schools with more than 40 attendees is ", schools  

schools = [item for item in d.keys() if d.get(item)>40]
print "Number of schools with more than 40 attendees is ", schools


# Create a new dictionary mapping attendance numbers to list of schools
# with that attendance number

d2 ={}
for (key,value) in d.iteritems():
    if value in d2:  # is it already a key in d2 
        d2[value].append(key)
    else:
        d2[value] = [key]
print d2


# create a new dictionary mapping groups of 50 attendees 0-49, 50-99, etc
d3 ={}
for (key,value) in d.iteritems():
    value = value/50 *50
    if value in d3:  # is it already a key in d2 
        d3[value].append(key)
    else:
        d3[value] = [key]
print d3