Mostly Linux & Python syntax notes and hyperlinks.

Friday, March 28, 2014

python: 1-d dictionary for counting heirarchical attributes

This dictionary method uses concatenated characters instead of sub-dictionaries to track the hierarchical counts. e.g Number of Dogs could be pets['D'], Number of red dogs could be pets['DR']. Number of red dogs living in Cambridge could be pets['DRC'].

What follows is the general dictionary-building method, followed by a method written to illustrate how it could be used, followed by the printout from the test method.
# Given a list of lists, build a set of keys that combine fields from each list
# return a 1-d dictionary of these keys initialized to input_default
#
# @param list_of_lists = list of key-lists - Should be single unique characters.
# @param input_default [optional] Value to initialize each dictionary entry
# if input_default parameter not given, then 0 is used
def build1Dictionary(list_of_lists,input_default=0):
    number_of_lists=len(list_of_lists)
    if number_of_lists==0:
        return dict()
    keylist=list_of_lists[0]
    if number_of_lists==1:
        return dict.fromkeys(keylist,input_default)
    for next_list in list_of_lists[1:]:
        new_list=list(keylist)
        for key in keylist:
            for xkey in next_list:
                new_list.append(key+xkey)
        keylist=new_list
    return dict.fromkeys(keylist,input_default)

# Here is a method to test the dictionary:
def count_pets():
    #Create the dictionary
    pets=['D','C','F','B']
    gender=['g','b']
    home=['R','S','U']
    words=dict(D="Dog",C="Cat",F="Fish",B="Bird",\
       g="Girl",b="Boy",R="Rural",S="Suburban",U="City")
    pets_dict=build1Dictionary([pets,gender,home])
   
    #fill the dictionary with some counts
    data=["DgS","DgS","DbR","CgU","FgS","CgR","BbR"]
    for pet in data:
        p=pet[0:1]
        g=pet[1:2]
        h=pet[2:3]
        pets_dict[p]+=1
        pets_dict[p+g]+=1
        pets_dict[p+g+h]+=1
   
    #Print the counts indented by level of heiarchy
    for p in pets:
        print ("%d %s(s)" % (pets_dict[p],words[p]))
        for g in gender:
            if pets_dict[p+g]>0:
                print ("  %d owned by a %s" % \
                   (pets_dict[p+g],words[g]))
                for h in home:
                    if pets_dict[p+g+h] > 0:
                        print("    %d in %s environment." % \
                            (pets_dict[p+g+h],words[h]))
   

if __name__ == '__main__':
    count_pets()
Then when I run it, it prints out:
3 Dog(s)
  2 owned by a Girl
    2 in Suburban environment.
  1 owned by a Boy
    1 in Rural environment.
2 Cat(s)
  2 owned by a Girl
    1 in Rural environment.
    1 in City environment.
1 Fish(s)
  1 owned by a Girl
    1 in Suburban environment.
1 Bird(s)
  1 owned by a Boy
    1 in Rural environment.

No comments:

Post a Comment