-1

I'm working with the following list:

["hello, how are you", "hello", "how are you doing","are you ok"]

how could I get the frequencies of every word inside of every element?

All in all the output of the list should look like this:

you: 3
are: 3
hello: 2
how: 2
doing: 1
ok: 1
Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
  • Possible duplicate of [How to count word frequencies within a file in python](https://stackoverflow.com/questions/12117576/how-to-count-word-frequencies-within-a-file-in-python) – bobrobbob Jul 04 '18 at 09:53
  • @bobrobbob the question are completely different, I'm dealing with a list and the question that you've flagged me with concerns text files. – Nazim Kerimbekov Jul 04 '18 at 09:57
  • that's not a difference. the logic behind is exactly the same. there are lots of questions in SO regarding the count word problem, it looks very weird to me that not one of them could help you one way or another – bobrobbob Jul 04 '18 at 09:59
  • @bobrobbob yeah for sure, some of them have helped me, but they all involved converting my list into a string then counting all the words, which in my opinion isn't the most optimum way. Therefore I wanted to see if there is a specific library or way to do it. – Nazim Kerimbekov Jul 04 '18 at 10:07
  • I would also advise you to take a look at the StackOverflow [help about duplicated](https://stackoverflow.com/help/duplicates). Which specifically states that "There are many ways to ask the same question, and a user might not be able to find the answer if they're asking it a different way". – Nazim Kerimbekov Jul 04 '18 at 10:10

3 Answers3

0

Using collections.Counter

Ex:

from collections import Counter
import string

data = ["hello, how are you", "hello", "how are you doing","are you ok"]
translator = str.maketrans('', '', string.punctuation)

d = Counter(" ".join(data).translate(translator).split())
#If python2
#d = Counter(" ".join(data).translate(None, string.punctuation).split())

print(d)

Output:

Counter({'are': 3, 'you': 3, 'how': 2, 'hello': 2, 'doing': 1, 'ok': 1})
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

You can use collections.Counter

from intertools import Counter
import string

l=["hello, how are you", "hello", "how are you doing","are you ok"]

Counter([w.strip(string.punctuation) for s in l for w in s.split() ])
# Counter({'are': 3, 'you': 3, 'hello': 2, 'how': 2, 'doing': 1, 'ok': 1})
Sunitha
  • 11,777
  • 2
  • 20
  • 23
0
def wordListToFreqDict(wordlist):
    wordfreq = [wordlist.count(p) for p in wordlist]
    return dict(zip(wordlist,wordfreq))

def sortFreqDict(freqdict):
    aux = [(freqdict[key], key) for key in freqdict]
    aux.sort()
    aux.reverse()
    return aux

a = ["hello, how are you", "hello", "how are you doing","are you ok"]
wordstring = ' '.join(a)
wordlist = wordstring.split()

wordfreq = [wordlist.count(w) for w in wordlist] # a list comprehension

dictionary = wordListToFreqDict(wordlist)
sorteddict = sortFreqDict(dictionary)

for s in sorteddict: print(str(s))

Result: (3, 'you') (3, 'are') (2, 'how') (1, 'ok') (1, 'hello,') (1, 'hello') (1, 'doing')

Boris Reif
  • 123
  • 1
  • 10