Appropriate data structure for deeply nested data

data structures

I can't figure out what would be an appropriate data structure for my problem. I need to write a script that will count some events. Input is a list of events (thousands of them). Each event is an object with following attributes:

class Event
  company
  city
  office
  department

You can look at it like this: „Each event is related to one company in a particular town in one office and in one department of that office“.

My script needs to output the total number of events for company, town office and department. Basically it needs to output sentences like this:

Total events in company X in town Y in office Z in department T : 15
Total events in company X in town Y in office Z in department R: 36
...
Total events in company N in town Y in office G in department T: 89
...

How should I store these counts and how should I count them. I was thinking of using nested dictionaries (in python) or in-memory SQL database. Is there any other way?

Best Answer

If the example sentence is ONLY required output, then it would be good to create a single key from all the sub-keys (company, town, office, department) and then use it as lookup for dictionary, whose value would be count of occurrence of this key.

If there are different aggregate outputs (counts, averages, over different keys) then you are probably looking for some kind of OLAP tool.

Related Topic