Python: count specific occurrences in a dictionary
Question: Say I have a dictionary like this:

    
    
    d={
    '0101001':(1,0.0), 
    '0101002':(2,0.0),
    '0101003':(3,0.5),
    '0103001':(1,0.0),
    '0103002':(2,0.9),
    '0103003':(3,0.4),
    '0105001':(1,0.0),
    '0105002':(2,1.0),
    '0105003':(3,0.0)}
    

Considering that the first four digits of each key consitute the identifier of
a "slot" of elements (e.g., '0101', '0103', '0105'), how can I count the
number of occurrences of `0.0` for each slot?

The intended outcome is a dict like this:

    
    
    result={
    '0101': 2,
    '0103': 1,
    '0105': 2} 
    

Apologies for not being able to provide my attempt as I don't really know how
to do this.


Answer: Use a [Counter](https://docs.python.org/2/library/collections.html#counter-
objects), add the first four digits of the key if the value is what you're
looking for:

    
    
    from collections import Counter
    
    counts = Counter()
    
    for key, value in d.items():
        if value[1] == 0.0:
            counts[key[:4]] += 1
    
    print counts
    

