title: Find *most* common prefix of strings - a better way?
body: <p>I have a list of keys <code>['foo_a','foo_b','foo_c','fnord']</code></p>
  
  <p>All of the similar solutions here assume that you have no <code>fnord</code>'s in your text.</p>
  
  <p>I have this code that does the job:</p>
  
  <pre><code>def detect_prefix(keys):
      PCT = 0.70 # cutof 
      pre = ''
      l = len(keys)
      for i in range(0, len(max(keys, key=len))):
          keys = filter(lambda k: k.startswith(pre), keys)
          cnt = dict()
          for k in map(lambda k: k[i], keys):
              cnt.setdefault(k,0)
              cnt[k] +=1
          if cnt[max(cnt)] / float(l) &gt;= PCT:
              pre += max(cnt)
          else:
              break
      return pre
  </code></pre>
  
  <p>I have a <em>strong</em> suspicion that this could be done much more elegantly, but my python-fu is not strong enough right now.</p>
  
  <p>I would love to hear some suggestions.</p>
  
  <p><strong>Edit</strong>.
  Additional background and clarification.<br>
  These are keys that other developers have put in an application to use for translation.
  They <em>should</em> have a common prefix, but people forget, and they cut and paste from other code. "_" as a prefix separator is just a convention. It would be best not to assume a separator is even used. 70% is a totally arbitrary threshold. "most prevalent" or "predominant" would have worked too.<br>
  And yes, this is python 2.7, and the space inside the quotes is just a visual artefact.</p>