Ranking python dictionary by percentile
<p>If i have a dictionary that records the count frequency of random objects:</p>

<pre><code>dict = {'oranges': 4 , 'apple': 3 , 'banana': 3 , 'pear' :1, 'strawberry' : 1....}
</code></pre>

<p>And I want only the keys that are in the top 25th percentile by frequency, how would i do that ? Especially if it's a very long tail list and a lot of records will have the same count. </p>

<p>Use a <a href="https://docs.python.org/2/library/collections.html#collections.Counter" rel="nofollow"><code>collections.Counter</code></a> object and exploit its <a href="https://docs.python.org/2/library/collections.html#collections.Counter.most_common" rel="nofollow"><code>most_common</code></a> method to return the keys with the highest frequency up to the required percentile.</p>

<p>For the 25th percentile, divide the length of the dictionary by 4 and pass that value to <code>most_common</code>:</p>

<pre><code>&gt;&gt;&gt; from collections import Counter
&gt;&gt;&gt; dct = {'oranges': 4 , 'apple': 3 , 'banana': 3 , 'pear' :1, 'strawberry' : 1}
&gt;&gt;&gt; c = Counter(dct)
&gt;&gt;&gt; [tup[0] for tup in c.most_common(len(dct)//4)]
['oranges']
</code></pre>

<p>Note that potential elements in that percentile with equal frequencies will be selected <em>arbitrarily</em>.</p>

python numpy dictionary