簡體   English   中英

在 python 3 中的字符串中查找單詞的出現

[英]Finding occurrences of a word in a string in python 3

我正在嘗試查找字符串中某個單詞的出現次數。

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

我使用以下方法來計算出現次數:

count = str1.count(word)

問題是我想要一個完全匹配的。 所以這句話的計數是0。這可能嗎?

如果您要提高效率:

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

這不需要創建任何中間列表(與split()不同),因此對於較大的input_string值可以有效地工作。

它還具有正確使用標點符號的好處 - 它會正確返回1作為短語"Mike saw a dog." (而沒有參數的split()不會)。 它使用\b正則表達式標志,它匹配單詞邊界( \w aka [a-zA-Z0-9_]和其他任何東西之間的轉換)。

如果您需要擔心 ASCII 字符集以外的語言,您可能需要調整正則表達式以正確匹配這些語言中的非單詞字符,但對於許多應用程序來說,這將過於復雜,並且在許多其他情況下設置 unicode 和/ 或正則表達式的語言環境標志就足夠了。

您可以使用str.split()將句子轉換為單詞列表:

a = 'the dogs barked'.split()

這將創建列表:

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

然后,您可以使用list.count()計算確切出現的次數:

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

如果它需要使用標點符號,您可以使用正則表達式。 例如:

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

使用列表推導:

>>> 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()返回一個句子中所有單詞的列表。 然后我們使用列表推導來計算單詞在句子中出現的次數。

import re

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

您需要將句子拆分為單詞。 對於你的例子,你可以做到這一點

words = str1.split()

但是對於真正的單詞使用,您需要更高級的東西來處理標點符號。 對於大多數西方語言,您可以在執行str1.split()之前用空格替換所有標點符號。

這在簡單的情況下也適用於英語,但請注意“I'm”將被拆分為兩個詞:“I”和“m”,實際上它應該被拆分為“I”和“am”。 但這對於這個應用程序來說可能是多余的。

對於其他情況,例如亞洲語言或英語在現實世界中的實際使用,您可能希望使用一個為您進行分詞的庫。

然后你有一個單詞列表,你可以做

count = words.count(word)
    #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
>>> 

創建接受兩個輸入的函數,即文本和單詞的句子。 將句子的文本拆分成列表中的單詞段,然后檢查要統計的單詞是否存在於被分割的單詞中,並將出現次數作為函數的返回。

如果你不需要RegularExpression那么你可以做這個巧妙的技巧

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

下面是一個簡單的例子,我們可以用新詞替換所需的詞,也可以替換所需的出現次數:

import string

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

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

輸出將是: +++ +++ +++

函數中的第一個參數是 search_string。 第二個是 new_string,它將替換您的 search_string。 第三個也是最后一個是出現次數。

讓我們考慮示例s = "suvotisuvojitsuvo" 如果你想計算不同的計數“suvo”和“suvojit”,那么你使用count()方法......計算不同的ie)你不計算suvojit到suvo..只計算孤獨的“suvo”。

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

然后找到你必須從 suvojit 計數中否定的孤獨 suvo 計數。

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

這將是我在評論的幫助下的解決方案:

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")

如果你想在 sting 中找到特定單詞的確切出現次數並且不想使用任何計數功能,那么可以使用以下方法。

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.")

這是一個使用 split 函數的簡單 python 程序

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))

我剛剛開始學習一般的編碼,我不知道任何這樣的庫。

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)  

另一種方法是通過標記字符串(分解成單詞)

使用 Python 標准庫的集合模塊中的計數器

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM