pythonpython-2.7dictionary

Multiple levels of keys and values in Python


I wanted to know if the functionality I am trying to implement in python is possible.

I have a global hash called Creatures. Creatures contain sub-hashes called mammals, amphibians, birds, insects.

Mammals have sub-hashes called whales, elephants. Amphibians have sub-hashes called frogs, larvae. Birds have sub-hashes called Eagle, parakeet. Insects have sub-hashes called dragonfly, mosquito.

Again, Eagles have sub-hashes called male, female.

I am counting the frequencies of all these creatures from a text file. For example, if the file is in below format:

Birds   Eagle  Female
Mammals whales Male
Birds   Eagle  Female

I should output Creatures[Birds[Eagle[Female]]] = 2
                Creatures[mammals[Whales[Male]]] = 1  

Is it possible in Python? How can it be done? I am very new to Python and please help is much appreciated. I am comfortable with dictionaries only up to 1 level, i.e. key-> value. But here, there are multiple keys and multiple values. i am not sure how to proceed with this. I am using Python 2.6.


Solution

  • If you just have to "count" things -- and assuming the data file contains all the required level of "hashes" -- that will do the trick:

    import collections
    
    result = collections.defaultdict(int)
    
    with open("beast","rt") as f:
        for line in f:
            hashes = line.split()
            key = '-'.join(hashes)
            result[key] += 1
    
    print result
    

    Producing the result:
    defaultdict(<type 'int'>, {'Mammals-whales-Male': 1, 'Birds-Eagle-Female': 2})

    If you require nested dictionary -- post-processing of that result is still possible...