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() 函数来获取可迭代项目(字典散列)的频率。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP