[Tutor] Python Block

Terry Peppers peppers at gmail.com
Fri Aug 11 16:26:09 CEST 2006

I'm really blocked right now, and I don't want to post this to the
main Python list since I think the answer is probably right in front
of my face. I'm clearly just too close to see it.

I'm able to do frequency counts of items in a list by doing the following:

>>> list = ["5100", "5100", "5100", "5200", "5200"]
>>> count = {}
>>> for l in list:
...     count[l] = count.get(l, 0) + 1
>>> count
{'5200': 2, '5100': 3}

Hooray, right?! However, my real problem is dealing with another
dimension of data. So suppose I can access the following data:

"5100", "foo"
"5100", "-"
"5100", "-"
"5100", "-"
"5200", "foo"
"5200", -

That's a total of 6  line records that I want to output like this:

5100        -         3
5100       foo      1
5200       -          1
5200       foo      1

I want to count the frequency of the second column of data that I can
access. I can't seem to get any traction as how to figure this out.
Should I put the data in a list of dictionaries? A dictionary of
lists? How do I call the data if there are all kinds of duplicate
keys? When the data's in some data structure how do I iterate over it
to get frequency of column B?

Really confused. Can someone help?

More information about the Tutor mailing list