-1

I have a list which contains words. I want to know the most frequent words in my list. I tried using 'counter' from collections package.

result = Counter(z).most_common(5)

and I got this result.

result
>>[('abc', 893), ('op', 198), ('bff', 172), ('ppf', 140), ('request', 119)]

but I only want the words and not the frequency no. attached with it. like

['abc','op','bff','ppf','request']
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
Shubham Ringne
  • 403
  • 2
  • 6
  • 11

3 Answers3

0

Use list comprehension to extract them from result:

print([res[0] for res in result])
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
0

You can use the index and list comprehension:

result = Counter(z).most_common(5)
result = [i[0] for i in result]

Or in one line:

result = [i[0] for i in Counter(z).most_common(5)]
Farhan.K
  • 3,425
  • 2
  • 15
  • 26
0

from HERE Counter is slower than defaultdict. if performance is important try this:

import operator
from collections import defaultdict

counter = defaultdict(int)
foods = ['soy', 'dairy', 'gluten', 'soy']
for k in foods:
    counter[k] += 1
most_common = 5
# counter includes a dictionary(without order)
# here we sort dictionary(this may needs more time than above link shows):
result = list(zip(*sorted(counter.items(), key=operator.itemgetter(1), reverse=True)[:most_common]))[0]
Alireza Afzal Aghaei
  • 1,184
  • 10
  • 27
  • It's output order is random .. I got output as : `['gluten', 'dairy']` But it should be `['soy', 'dairy']` or `['soy', 'gluten']` and When I try to print counter :`print(counter)` , I got output : `defaultdict(, {'gluten': 1, 'soy': 2, 'dairy': 1})` – Kalpesh Dusane Aug 30 '16 at 13:17
  • Can we use normal dictionary instead of defaultdict? I tried and it works : `counter = {} foods = ['soy', 'dairy', 'gluten', 'soy'] for k in foods: if k in counter : counter[k] += 1 else: counter[k] = 1` Is there any difference in the performance? – Kalpesh Dusane Aug 31 '16 at 05:30
  • @KalpeshDusane defaultdict is a normal dict with default value(`int == 0`) see [this link](http://stackoverflow.com/questions/6589814/what-is-the-difference-between-dict-and-collections-defaultdict). your code works but i think checking conditions are slow:) – Alireza Afzal Aghaei Aug 31 '16 at 07:38