Python程式:統計包含特定字母的單詞數量
在本文中,我們將學習如何在Python中統計字串中包含特定字母的單詞數量。
使用的方法
以下是完成此任務的各種方法:
使用列表推導式、len() 和 split() 函式
使用 split() 和 find() 函式
使用 split()、replace() 和 len() 函式
使用 Counter() 函式
示例
假設我們已經獲取了一個**輸入字串**和一些隨機的**字元**。我們現在將統計輸入字串中包含給定輸入字元的單詞數量。
輸入
inputString = 'hello tutorialspoint python codes' inputChar = "p"
輸出
Count of words containing the char 'p' is: 2
在上面的字串中,包含輸入字元“**p**”的單詞是**tutorialspoint** 和 **python**。因此輸出為2。
方法1:使用列表推導式、len() 和 split() 函式
列表推導式
當您希望根據現有列表的值構建一個新的列表時,列表推導式提供了一種更短/簡潔的語法。
**len()** - len() 方法返回物件中的專案數。當物件是字串時,len() 函式返回字串中的字元數。
**split()** - 將字串拆分為列表。我們可以定義分隔符;預設分隔符是任何空白字元。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
建立一個變數來儲存輸入字串。
列印輸入列表。
建立另一個變數來儲存輸入字元。
使用 split() 函式將輸入字串拆分為單詞列表,並在該列表中遍歷,然後檢查當前列表元素中是否存在輸入字元。
列印輸入字串中包含給定輸入字元的單詞數量。
示例
以下程式使用列表推導式、len() 和 split() 函式返回輸入字串中包含給定輸入字元的單詞數量:
# input string inputString = 'hello tutorialspoint python codes' # printing input string print("Input String:", inputString) # input character inputChar = "p" # splitting the input string into a list of words and traversing in that list # and then checking if the input char is present in the current list element wordsCount = len([element for element in inputString.split() if inputChar in element]) # printing the count of words containing the input character print("The Count of words containing the char 'p' is:", wordsCount)
輸出
執行以上程式後,將生成以下輸出:
Input String: hello tutorialspoint python codes The count of words containing the char 'p' is: 2
方法2:使用 split() 和 find() 函式
**find() 方法** - 查詢給定值的第一次出現。如果未找到該值,則此方法返回 -1。
語法
string.find(value, start, end)
示例
以下程式使用 split() 和 find() 函式返回輸入字串中包含給定輸入字元的單詞數量:
# input string inputString = 'hello tutorialspoint python codes' # printing input string print("Input String:", inputString) # input character inputChar = "p" # storing the words count with the input character wordsCount=0 # splitting input string into the list of words wordsList= inputString.split() # traversing in that words list for element in wordsList: # checking whether input char is present in the current list element if(element.find(inputChar)!=-1): # incrementing word count value by 1 if the condition is true wordsCount+=1 print("The Count of words containing the char 'p' is:", wordsCount)
輸出
執行以上程式後,將生成以下輸出:
Input String: hello tutorialspoint python codes The count of words containing the char 'p' is: 2
方法3:使用 split()、replace() 和 len() 函式
**replace() 函式** - 返回字串的副本,該副本將舊子字串的所有出現都替換為另一個新子字串。
語法
string.replace(old, new, count)
示例
以下程式使用 split()、replace() 和 len() 函式返回輸入字串中包含給定輸入字元的單詞數量:
# input string inputString = 'hello tutorialspoint python codes' # printing input string print("Input String:", inputString) # input character inputChar = "p" # storing the words count with the input character wordsCount=0 # splitting input string into the list of words wordsList= inputString.split() # traversing in that words list for element in wordsList: # replacing given input char with space and storing that string p = element.replace(inputChar, "") # incrementing the words count by 1 if the length of the above string # is less than the length of the current element if(len(p) < len(element)): wordsCount += 1 print("The Count of words containing the char 'p' is:", wordsCount)
輸出
Input String: hello tutorialspoint python codes The count of words containing the char 'p' is: 2
方法4:使用 Counter() 函式
**Counter() 函式** - 一個子類,用於統計可雜湊物件。它在被呼叫/呼叫時隱式地建立可迭代物件的雜湊表。
在此方法中,Counter() 函式返回輸入字串中每個單詞的字元頻率。
示例
以下程式使用 Counter() 函式返回輸入字串中包含給定輸入字元的單詞數量:
# importing a Counter function from the collections module from collections import Counter # input string inputString = 'hello tutorialspoint python codes' # printing input string print("Input String:", inputString) # input character inputChar = "p" # storing the words count with the input character wordsCount = 0 # splitting input string into the list of words wordsList = inputString.split() # traversing through the elements of the words list for element in wordsList: # getting the frequency of characters of the current word as a key-value pair ele_frequency = Counter(element) # checking whether the input char is present in the keys of the frequency dictionary if inputChar in ele_frequency.keys(): # incrementing the words count by 1 if the condition is true wordsCount += 1 print("The Count of words containing the char 'p' is:", wordsCount)
輸出
Input String: hello tutorialspoint python codes The count of words containing the char 'p' is: 2
結論
在本文中,我們研究了四種不同的方法來統計以特定字母開頭的單詞。我們還學習瞭如何使用 Counter() 函式來獲取可迭代專案(字典雜湊)的頻率。