title: Choosing highest occuring value from python dictionary
body: <p>I have following sample data:</p>
  
  <pre><code>myTypeDict = {
                'ssf': ['ssf', 'short film', 'film','artwork', 'creative work', 'entity'],
                'short film': ['short film', 'film', 'artwork', 'creative work', 'work', 'entity'],
                'silent film': ['silent film', 'film', 'artwork', 'creative work', 'work', 'entity'],
                'island': ['island', 'landform', 'landscape', 'pysio-geographical object', 'geographical object', 'entity'],
                'peninsula': ['peninsula', 'landform', 'landscape', 'pysio-geographical object', 'geographical object', 'entity'],
              }
  </code></pre>
  
  <p>Values in this dictionary are actually ancestors of the key. For each key, I have 5 ancestors. I want to have another dictionary containing this same key, and value as the parent with the highest occurances among all key. E.g. I want output as:</p>
  
  <pre><code>{
      'ssf': film,
      'short film': film,
      'silent film': film,
      'island': landform,
      'peninsula': landform
  }
  </code></pre>
  
  <p>Since, the film is occurring 3 times in complete dictionary, I will substitute this as parent for <code>'ssf', 'short film', 'silent film'</code> etc. Moreover, I need the earliest parent. i.e. even <code>creative work</code> is also occurring 3 times, but it is ancestor of <code>film</code>. So if the two ancestors occurring same number of times, then I need the earliest one i.e. here 'film'. Similarly for island and peninsula, I need landform. I also can somehow get descendants of each key in nested dictionary format. For this I have following code:</p>
  
  <pre><code>myPredicate= URIRef("http://www.w3.org/2000/01/rdf-schema#subClassOf")
      roots = set()
      mapping = {}
      parents = {} #to store parents
      count = 0
      for s,p,o in g:
          if(p==myPredicate):
              count = count+1
              s = URIRef(s).n3().replace('&lt;http://www.wikidata.org/entity/',"")
              child = s.replace('&gt;',"")
              o = URIRef(o).n3().replace('&lt;http://www.wikidata.org/entity/',"")
              parent = o.replace('&gt;',"")
              parents[child] = parent #populate parents       
              childitem = mapping.get(child,None)
              if childitem is None:
                  childitem =  {}
                  mapping[child] = childitem
              else:
                  roots.discard(child)
              parentitem = mapping.get(parent,None)
              if parentitem is None:
                  mapping[parent] = {child:childitem}
                  roots.add(parent)
              else:
                  parentitem[child] = childitem
  </code></pre>
  
  <p>The dictionary 'mapping' is actually tree. So when I say mapping[key], it gives me subtree rooted as node 'key'. If I could count this nodes in subtree then it'll resolve my issue I guess. Anyone knows how can I count keys in nested dictionary.</p>