title: Is there a better way to convert tokens to features in Python?
body: <p>I wrote a Python function following a pseudo-code to convert tokens to features. What it does is to return the most-appeared k tokens. I am wondering if there is any better/nicer way to write the function. </p>
  
  <pre><code>def token2feature(k,ts): #k is number of desired features; ts is a list of all tokens
     import operator
     hs={} #create an empty dictionary
     for tok in ts:
         hs[tok] =hs[tok]+1 if tok in hs else 1
     sorted_hs=sorted(hs.items(),key=operator.itemgetter(1),reverse=True)
     return dict(sorted_hs[:k]).keys()
  </code></pre>
  
  <p>Edited:<br>
  Input is a list of tokens</p>
  
  <pre><code>ts=['a','b','a','a','b'','c']
  k=2
  </code></pre>
  
  <p>Output is the most occurred k elements in the list. </p>
  
  <pre><code>['a','b']
  </code></pre>