4

consider

doc = ["i am a fellow student", "we both are the good student", "a student works hard"]

I have this as input I just wanted to print the number of times each word in the whole list occurs:

For example student occurs 3 times so expected output student=3, a=2,etc

I was able to print the unique words in the doc, but not able to print the occurrences. Here is the function i used:

def fit(doc):    
    unique_words = set() 
    if isinstance(dataset, (list,)):
        for row in dataset:
            for word in row.split(" "): 
                if len(word) < 2:
                    continue
                unique_words.add(word)
        unique_words = sorted(list(unique_words))
        return (unique_words)
doc=fit(docs)

print(doc)

['am', 'are', 'both', 'fellow', 'good', 'hard', 'student', 'the', 'we', 'works']

I got this as output I just want the number of occurrences of the unique_words. How do i do this please?

SpoonMeiser
  • 19,918
  • 8
  • 50
  • 68
tachyon
  • 47
  • 1
  • 6
  • Look up `collections.Counter`. – tzaman Jul 07 '20 at 12:22
  • Since you made list of unique words, you can loop through the **doc** again and make a new dictionary which has key as its word and value of no of times word appears, or you could even do nested list!! – Agent_Orange Jul 07 '20 at 12:24

6 Answers6

7

You just need to use Counter, and you will solve the problem by using a single line of code:

from collections import Counter

doc = ["i am a fellow student",
       "we both are the good student",
       "a student works hard"]

count = dict(Counter(word for sentence in doc for word in sentence.split()))

count is your desired dictionary:

{
    'i': 1,
    'am': 1,
    'a': 2,
    'fellow': 1,
    'student': 3,
    'we': 1,
    'both': 1,
    'are': 1,
    'the': 1,
    'good': 1,
    'works': 1,
    'hard': 1
}

So for example count['student'] == 3, count['a'] == 2 etc.

Here it's important to use split() instead of split(' '): in this way you will not end up with having an "empty" word within count. Example:

>>> sentence = "Hello     world"
>>> dict(Counter(sentence.split(' ')))
{'Hello': 1, '': 4, 'world': 1}
>>> dict(Counter(sentence.split()))
{'Hello': 1, 'world': 1}
Riccardo Bucco
  • 13,980
  • 4
  • 22
  • 50
3

Use

from collections import Counter
Counter(" ".join(doc).split())

results in

Counter({'i': 1,
         'am': 1,
         'a': 2,
         'fellow': 1,
         'student': 3,
         'we': 1,
         'both': 1,
         'are': 1,
         'the': 1,
         'good': 1,
         'works': 1,
         'hard': 1})

Explanation: first create one string by using join and split it on spaces with split to have a list of single words. Use Counter to count the appearances of each word

pythonic833
  • 3,054
  • 1
  • 12
  • 27
3
doc = ["i am a fellow student", "we both are the good student", "a student works hard"]

p = doc[0].split() #first list    
p1 = doc[1].split() #second list    
p2 = doc[2].split() #third list

f1 = p + p1 + p2

j = len(f1)-1

n = 0

while n < j:    
    print(f1[n],"is found",f1.count(f1[n]), "times")    
    n+=1
General Grievance
  • 4,555
  • 31
  • 31
  • 45
2

You can use set and a string to aggregate all word in each sentence after that to use dictionary comprehension to create a dictionary by the key of the word and value of the count in the sentence

doc = ["i am a fellow student", "we both are the good student", "a student works hard"]
uniques = set()
all_words = ''
for i in doc:
    for word in i.split(" "):
        uniques.add(word)
        all_words += f" {word}"
print({i: all_words.count(f" {i} ") for i in uniques})

Output

{'the': 1, 'hard': 0, 'student': 3, 'both': 1, 'fellow': 1, 'works': 1, 'a': 2, 'are': 1, 'am': 1, 'good': 1, 'i': 1, 'we': 1}
Leo Arad
  • 4,452
  • 2
  • 6
  • 17
1

Thanks for Posting in Stackoverflow I have written a sample code that does what you need just check it and ask if there is anything you don't understand

doc = ["i am a fellow student", "we both are the good student", "a student works hard"]

checked = []
occurence = []
for sentence in doc:
    for word in sentence.split(" "):
        if word in checked:
            occurence[checked.index(word)] = occurence[checked.index(word)] + 1
        else:
            checked.append(word)
            occurence.append(1)
for i in range(len(checked)):
    print(checked[i]+" : "+str(occurence[i]))
Mohamed Nashaat
  • 90
  • 2
  • 11
0

try this one

   doc = ["i am a fellow student", "we both are the good student", "a student works hard"]
words=[]
for a in doc:
    b=a.split()
    for c in b:
        #if len(c)>3: #most words there length > 3 this line in your choice
            words.append(c)
wc=[]
for a in words:
    count = 0
    for b in words:
        if a==b :
            count +=1
    wc.append([a,count])
print(wc)