lost9123193
lost9123193

Reputation: 11030

Finding occurrences of a word in a string in python 3

I'm trying to find the number of occurrences of a word in a string.

word = "dog"
str1 = "the dogs barked"

I used the following to count the occurrences:

count = str1.count(word)

The issue is I want an exact match. So the count for this sentence would be 0. Is that possible?

Upvotes: 35

Views: 102313

Answers (14)

Tarun Kumar
Tarun Kumar

Reputation: 875

Another way to do this is by tokenizing string (breaking into words)

Use Counter from collection module of Python Standard Library

from collections import Counter 

str1 = "the dogs barked"
stringTokenDict = { key : value for key, value in Counter(str1.split()).items() } 

print(stringTokenDict['dogs']) 
#This dictionary contains all words & their respective count 

Upvotes: 0

Bharath R
Bharath R

Reputation: 1

I have just started out to learn coding in general and I do not know any libraries as such.

s = "the dogs barked"
value = 0
x = 0
y=3
for alphabet in s:
    if (s[x:y]) == "dog":
        value = value+1
    x+=1
    y+=1
print ("number of dog in the sentence is : ", value)  

Upvotes: 0

Rutuja
Rutuja

Reputation: 321

This is simple python program using split function

str = 'apple mango apple orange orange apple guava orange'
print("\n My string ==> "+ str +"\n")
str = str.split()
str2=[]

for i in str:
     if i not in str2:
         str2.append(i)
         print( i,str.count(i))

Upvotes: 0

wgetDJ
wgetDJ

Reputation: 1239

If you want to find the exact number of occurrence of the specific word in the sting and you don't want to use any count function, then you can use the following method.

text = input("Please enter the statement you want to check: ")
word = input("Please enter the word you want to check in the statement: ")

# n is the starting point to find the word, and it's 0 cause you want to start from the very beginning of the string.
n = 0

# position_word is the starting Index of the word in the string
position_word = 0
num_occurrence = 0

if word.upper() in text.upper():
    while position_word != -1:
        position_word = text.upper().find(word.upper(), n, len(text))

        # increasing the value of the stating point for search to find the next word
        n = (position_word + 1)

        # statement.find("word", start, end) returns -1 if the word is not present in the given statement. 
        if position_word != -1:
            num_occurrence += 1

    print (f"{word.title()} is present {num_occurrence} times in the provided statement.")

else:
    print (f"{word.title()} is not present in the provided statement.")

Upvotes: 0

HeavenHM
HeavenHM

Reputation: 992

If you don't need RegularExpression then you can do this neat trick.

word = " is " #Add space at trailing and leading sides.
input_string = "This is some random text and this is str which is mutable"
print("Word count : ",input_string.count(word))
Output -- Word count :  3

Upvotes: 1

Boniphace Udoya
Boniphace Udoya

Reputation: 11

    #counting the number of words in the text
def count_word(text,word):
    """
    Function that takes the text and split it into word
    and counts the number of occurence of that word
    input: text and word
    output: number of times the word appears
    """
    answer = text.split(" ")
    count = 0
    for occurence in answer:
        if word == occurence:
            count = count + 1
    return count

sentence = "To be a programmer you need to have a sharp thinking brain"
word_count = "a"
print(sentence.split(" "))
print(count_word(sentence,word_count))

#output
>>> %Run test.py
['To', 'be', 'a', 'programmer', 'you', 'need', 'to', 'have', 'a', 'sharp', 'thinking', 'brain']
2
>>> 

Create the function that takes two inputs which are sentence of text and word. Split the text of a sentence into the segment of words in a list, Then check whether the word to be counted exist in the segmented words and count the occurrence as a return of the function.

Upvotes: 1

roger
roger

Reputation: 23

This would be my solution with help of the comments:

word = str(input("type the french word chiens in english:"))
str1 = "dogs"
times = int(str1.count(word))
if times >= 1:
    print ("dogs is correct")
else:
    print ("your wrong")

Upvotes: 0

Maxx Selva K
Maxx Selva K

Reputation: 512

Let us consider the example s = "suvotisuvojitsuvo". If you want to count no of distinct count "suvo" and "suvojit" then you use the count() method... count distinct i.e) you don't count the suvojit to suvo.. only count the lonely "suvo".

suvocount = s.count("suvo") // #output: 3
suvojitcount = s.count("suvojit") //# output : 1

Then find the lonely suvo count you have to negate from the suvojit count.

lonelysuvo = suvocount - suvojicount //# output: 3-1 -> 2

Upvotes: 0

abhay goyan
abhay goyan

Reputation: 11

Below is a simple example where we can replace the desired word with the new word and also for desired number of occurrences:

import string

def censor(text, word):<br>
    newString = text.replace(word,"+" * len(word),text.count(word))
    print newString

print censor("hey hey hey","hey")

output will be : +++ +++ +++

The first Parameter in function is search_string. Second one is new_string which is going to replace your search_string. Third and last is number of occurrences .

Upvotes: 0

Aaron
Aaron

Reputation: 51

import re

word = "dog"
str = "the dogs barked"
print len(re.findall(word, str))

Upvotes: 5

TerryA
TerryA

Reputation: 59974

Use a list comprehension:

>>> word = "dog"
>>> str1 = "the dogs barked"
>>> sum(i == word for word in str1.split())
0

>>> word = 'dog'
>>> str1 = 'the dog barked'
>>> sum(i == word for word in str1.split())
1

split() returns a list of all the words in a sentence. Then we use a list comprehension to count how many times the word appears in a sentence.

Upvotes: 6

Lennart Regebro
Lennart Regebro

Reputation: 172239

You need to split the sentence into words. For you example you can do that with just

words = str1.split()

But for real word usage you need something more advanced that also handles punctuation. For most western languages you can get away with replacing all punctuation with spaces before doing str1.split().

This will work for English as well in simple cases, but note that "I'm" will be split into two words: "I" and "m", and it should in fact be split into "I" and "am". But this may be overkill for this application.

For other cases such as Asian language, or actual real world usage of English, you might want to use a library that does the word splitting for you.

Then you have a list of words, and you can do

count = words.count(word)

Upvotes: 3

Amber
Amber

Reputation: 526593

If you're going for efficiency:

import re
count = sum(1 for _ in re.finditer(r'\b%s\b' % re.escape(word), input_string))

This doesn't need to create any intermediate lists (unlike split()) and thus will work efficiently for large input_string values.

It also has the benefit of working correctly with punctuation - it will properly return 1 as the count for the phrase "Mike saw a dog." (whereas an argumentless split() would not). It uses the \b regex flag, which matches on word boundaries (transitions between \w a.k.a [a-zA-Z0-9_] and anything else).

If you need to worry about languages beyond the ASCII character set, you may need to adjust the regex to properly match non-word characters in those languages, but for many applications this would be an overcomplication, and in many other cases setting the unicode and/or locale flags for the regex would suffice.

Upvotes: 52

grc
grc

Reputation: 23555

You can use str.split() to convert the sentence to a list of words:

a = 'the dogs barked'.split()

This will create the list:

['the', 'dogs', 'barked']

You can then count the number of exact occurrences using list.count():

a.count('dog')  # 0
a.count('dogs') # 1

If it needs to work with punctuation, you can use regular expressions. For example:

import re
a = re.split(r'\W', 'the dogs barked.')
a.count('dogs') # 1

Upvotes: 18

Related Questions