Python dictionary with variables as keys
<p>I'm a Python newbie trying to parse a file to make a table of memory allocations. My input file is in the following format:</p>

<pre><code>48 bytes allocated at 0x8bb970a0
24 bytes allocated at 0x8bb950c0
48 bytes allocated at 0x958bd0e0
48 bytes allocated at 0x8bb9b060
96 bytes allocated at 0x8bb9afe0
24 bytes allocated at 0x8bb9af60    
</code></pre>

<p>My first objective is to make a table that counts the instances of a particular number of byte allocations. In other words, my desired output for the above input would be something like:</p>

<pre><code>48 bytes -&gt; 3 times
96 bytes -&gt; 1 times
24 bytes -&gt; 2 times
</code></pre>

<p>(for now, I'm not concerned about the memory addresses)</p>

<p>Since I'm using Python, I thought doing this using a dictionary would be the right way to go (based on about 3 hours' worth of reading Python tutorials). Is that a good idea?</p>

<p>In trying to do this using a dictionary, I decided to make the number of bytes the 'key', and a counter as the 'value'. My plan was to increment the counter on every occurrence of the key. As of now, my code snippet is as follows:</p>

<pre><code># Create an empty dictionary
allocationList = {}

# Open file for reading
with open("allocFile.txt") as fp: 
    for line in fp: 
        # Split the line into a list (using space as delimiter)
        lineList = line.split(" ")

        # Extract the number of bytes
        numBytes = lineList[0];

        # Store in a dictionary
        if allocationList.has_key('numBytes')
            currentCount = allocationList['numBytes']
            currentCount += 1
            allocationList['numBytes'] = currentCount
        else
            allocationList['numBytes'] = 1 

for bytes, count in allocationList.iteritems()
    print bytes, "bytes -&gt; ", count, " times"
</code></pre>

<p>With this, I get a syntax error in the 'has_key' call, which leads me to question whether it is even possible to use variables as dictionary keys. All examples I have seen so far assume that keys are available upfront. In my case, I can get my keys only when I'm parsing the input file.</p>

<p>(Note that my input file can run into thousands of lines, with hundreds of different keys)</p>

<p>Thank you for any help you can provide.</p>
