Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>David's solution is the best.</p> <p>But probably more for fun than anything, here is a solution that does not import any module:</p> <pre><code>dicto = {} for ele in mylist: try: dicto[ele] += 1 except KeyError: dicto[ele] = 1 top_10 = sorted(dicto.iteritems(), key = lambda k: k[1], reverse = True)[:10] </code></pre> <p>Result:</p> <pre><code>&gt;&gt;&gt; top_10 [('and', 13), ('all', 2), ('as', 2), ('borogoves', 2), ('boy', 1), ('blade', 1), ('bandersnatch', 1), ('beware', 1), ('bite', 1), ('arms', 1)] </code></pre> <p>EDIT:</p> <p>Answering the follow up question:</p> <pre><code>new_dicto = {} for val, key in zip(dicto.itervalues(), dicto.iterkeys()): try: new_dicto[val].append(key) except KeyError: new_dicto[val] = [key] alph_sorted = sorted([(key,sorted(val)) for key,val in zip(new_dicto.iterkeys(), new_dicto.itervalues())], reverse = True) </code></pre> <p>Result:</p> <pre><code>&gt;&gt;&gt; alph_sorted [(13, ['and']), (2, ['all', 'as', 'borogoves']), (1, ['"and', '"beware', '`twas', 'arms', 'awhile', 'back', 'bandersnatch', 'beamish', 'beware', 'bird', 'bite', 'blade', 'boy', 'brillig'])] </code></pre> <p>The words that show up once are sorted alphabetically, if you notice some words have extra quotation marks in them.</p> <p>EDIT:</p> <p>Answering another follow up question:</p> <pre><code>top_10 = [] for tup in alph_sorted: for word in tup[1]: top_10.append(word) if len(top_10) == 10: break </code></pre> <p>Result:</p> <pre><code>&gt;&gt;&gt; top_10 ['and', 'all', 'as', 'borogoves', '"and', '"beware', '`twas', 'arms', 'awhile', 'back'] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload