title: Python keep track of certain attribute counts group by some parent tags
body: <p>I need to keep track of the count of certain attributes in a child tag grouped by attribute from parent tag
  in the example below</p>
  <pre><code>  &lt;Unit ...&gt;
      &lt;Unit type=&quot;1&quot;&gt;
        &lt;Result code1=&quot;1&quot; code2=&quot;1&quot; /&gt;
      &lt;Unit type=&quot;2&quot;&gt;
        &lt;Result code1=&quot;1&quot; code2=&quot;1&quot; /&gt;
      &lt;Unit type=&quot;1&quot;&gt;
        &lt;Result code1=&quot;444&quot; code2=&quot;1&quot; /&gt;
      &lt;Unit type=&quot;1&quot;&gt;
        &lt;Result code1=&quot;444&quot; code2=&quot;1&quot; /&gt;
      &lt;Unit type=&quot;2&quot;&gt;
        &lt;Result code1=&quot;1025&quot; code2=&quot;1&quot; /&gt;
        .
        .
        .
        .
  </code></pre>
  <p>I need to be track sume of all result tags grouped by the value of code1 and code2 : final result something like</p>
  <p>code1 counts for Unit Type 1 :  {'1': 1, '444':2}
  code1 counts for Unit Type 2 :  {'1':1 , '1025':1}</p>
  <p>same for code2 attrib</p>
  <p>code2 counts for Unit Type 1 :  {'1': 3}
  code1 counts for Unit Type 2 :  {'1':2}</p>
  <p>same for the other attribute code2</p>
  <p>I used something like</p>
  <pre><code>for unit in root.iter('Unit'):
      #print(dut.attrib)
      unit_type = int(unit.get('type'))
  
      code1_map = {}
  
      
      code1_type_map[siteNumber] = {}
  
     for result in root.iter('Result'):
          code1 = int(result.get('code1'))
          code2 = int(result.get('code2'))
  
          if code1 in code1_map:
              code1_map[code1] +=1
          else:
              code1_map[code1] = 1
  
          code1_type_map[unit_type]= code1_map
  
  
  #print results:
  for key, value in code1_type_map.items():
      print(&quot;==&gt;site : value &quot; , key , ' , ' , value)
      for code1, cnt in value.items():
          print(&quot; code1 , count &quot; , code1 , ' , ' , cnt )
  </code></pre>
  <p>It seems to always give the total counts, no seperation by the value of attributes code1 and code2</p>
  <p>any help is appreciated.</p>