title: Indexing Heirarchial Data with Python
body: <p>I am looking for a more pythonic way or better practice to simplify filtering data. Basically, I have class-structured data, and I want to select and group attributes. Is there a good way to do this without flattening to a tabular format?</p>
  
  <p>Example:</p>
  
  <pre><code>class Point:
      def __init__(self, x, y)
          self.x = x
          self.y = y
  
  class Square:
      def __init__(self,origin,length,rotation,color):
          self.origin = origin
          self.length = length
          ...
  
  
  #populated with all squares
  square_list = get_squares()
  
  #create x,y map of all squares
  XYindex = defaultdict(list)
  for square in square_list:
      XYindex[str(square.origin.x)+','+str(square.origin.y)].append(square)
  
  
  colorindex = defaultdict(list)
  for square in square_list:
      colorindex[str(square.color)].append(square)
  
  #find black squares at 0,0
  result = []
  for square in colorindex['black']:
      if square in XYindex['0,0']:
          result.append(square)
  </code></pre>