网站搜索

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”的单词是 tutorialspointpython。因此输出为 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

结论

在本文中,我们研究了 4 种不同的方法来计算以特定字母开头的单词。我们还学习了如何使用 Counter() 函数来获取可迭代项的频率(字典哈希)

相关文章: