Mostly Linux & Python syntax notes and hyperlinks.

Friday, June 20, 2014

python: Links to my posts about python dictionary objects

purpose

The reason for my interest is that we generate output tab-separated files containing values that need to be tabulated into reports. I was looking for a way to generalize the code for creating these reports. In other words:
Given a list of strings describing the counts saved in each column of a CSV file, what would be the best way to automate the creation of reports of counts for individual and common categories?

For example, if a list of voters had values like Party Affiliation (D/R/O/U), Gender, Zip Code, etc, I should be able to feed in these values and get a report listing totals for Female Republicans voters[R][F], Male Democrats with Zip Code 12345 voters[D][M][12345], total unaffiliated voters voters[U], etc. And then I should be able to use the same tool to automate reports for my next list of zoo animals, with columns tabulating Species, Location, Diet...

input

  1. list of column ID's
  2. dictionary mapping column ID's to corresponding strings to use in report
  3. collection of totals desired for the report

output

  1. From input 1. and the CSV file you should be able to produce the dictionary structure containing all the totals.
  2. From 1 and 2 you should be able to produce a function to generate strings for a report line
  3. From 1, 2, 3 you should be able to produce a general sort of report. (I haven't bothered with that yet.)

links

I know there is a better way to organize this blog to enable easier searching by topic but I haven't investigated how yet. Until then, collected here are the posts where I was exploring the use of multidimensional dictionaries:
At some point I thought I had been clever, and posted the way to nest dictionaries. Later, I found that it did not work--my test data had had too many 1's and I hadn't caught that I was adding up the wrong values.

I ended up using a 1-D dictionary to save my multidimensional values. This worked so long as I could use a single character to express all the possibilities for each dimension, with no re-use.