单词有多少个 快速python统计单词出现次数数量的方法?

方法1: 统计空格数??问题:没考虑多个空格相连的情况方法2:如果一个空格紧跟一个非空格,一定是新单词开始this is my first test!问:第一个单词没算上?答:count 再加1就行!??再问:若第一个单词前有空格会怎样?就又多了1解决办法:if(a[0]!=' ') count++;方法3:如果一个非空格后是一个空格,一定是单词的结束this is my first test!问:最后一个单词没算上?答:count 再加1就行!??再问:若最后一个单词后有空格会怎样?就又多了1解决办法:if(a[len-1]!=' ') count++;程序设计;#include <iostream>
#include <cstring>
using namespace std;
int main()
{
int word=0,count=0;
char a[1001];
gets(a);
for(int i=0;a[i]!='\0';i++)
{
if(a[i]!=' '&&word==0)
//新单词出现
{
word=1;
count++;
}
if(a[i]==' ')
//遇到空格,word置0
word=0;
}
cout<<count<<endl;
}
如何在字符串 Python 中统计单词,下面我们来一起看几种方法:一、使用 split() 和 len() 方法计算 Python 字符串中的字数split() 是 Python 中的一个内置方法,它通过使用特定的分隔符来分隔字符串中的单词并返回一个字符串数组。此方法最多接受两个参数作为参数:separator(可选) - 它充当分隔符(例如逗号、分号、引号或斜线)。指定要在字符串中分隔的边界。如果未指定 separator,默认 separator 是任何空格(空格、换行符、制表符等)。maxsplit(可选) - 它定义了最大分割数。如果未定义 maxsplit 的默认值是 -1,这意味着它没有限制并将字符串拆分为多个块。split() 的语法:str.split(separator, maxsplit)len() 也是一个 Python 内置方法,它返回数组中字符串的数量或计算对象中项目的长度。此方法只接受一个参数:字符串、字节、列表、对象、集合或集合。如果参数丢失或无效,它将引发 TypeError 异常。len() 的语法:len(s)让我们看看 split() 和 len() 方法如何计算字符串中的单词数。示例 1:无参数# initialize string
text = 'The quick brown fox jumps over the lazy dog'
# default separator: space
result = len(text.split())
print("There are " + str(result) + " words.")输出:There are 9 words.示例 2:使用 separator 参数# initialize string
bucket_list = 'Japan, Singapore, Maldives, Europe, Italy, Korea'
# comma delimiter
result = len(bucket_list.split(','))
# Prints an array of strings
print(bucket_list.split(','))
print("There are " + str(result) + " words.")输出:['Japan', ' Singapore', ' Maldives', ' Europe', ' Italy', ' Korea']
There are 6 words.split() 方法将返回一个新的字符串列表,len() 计算列表中的字符串。示例 3:使用 separator 和 maxsplit 参数# initialize string
bucket_list = 'Japan, Singapore, Maldives, Europe, Italy, Korea'
# comma delimiter
result = len(bucket_list.split(',', 3))
# Prints an array of strings
print(bucket_list.split(',', 3))
print("There are " + str(result) + " words.")输出:['Japan', ' Singapore', ' Maldives', ' Europe, Italy, Korea']
There are 4 words.maxsplit 只拆分 bucket_list 中的前三个逗号。如果你设置了 maxsplit,列表就会有一个 maxsplit+1 项。输出:['Japan', ' Singapore', ' Maldives, Europe, Italy, Korea']
There are 3 words.split() 方法将大字符串分解成更小的字符串。因此,字符串数组中单词的计数将不完全基于单词,而是基于拆分分隔符的定义方式。二、使用 RegEx 模块计算 Python 字符串中的单词正则表达式,简称 regex 或 regexp,是一个非常强大的搜索和操作文本字符串的工具;这可用于数据预处理、验证目的、在文本字符串中查找模式等。正则表达式还可以帮助计算文本字符串中包含不需要的标点符号或特殊字符的情况下的单词数。Regex 是一个 Python 内置包,所以我们只需要导入包 re 即可开始使用它。# import regex module
import re
# initialize string
text = 'Python !! is the be1st $$
programming language @'
# using regex findall()
result = len(re.findall(r'\w+', text))
print("There are " + str(result) + " words.")输出:There are 6 words.三、使用 sum()、strip() 和 split() 方法计算 Python 字符串中的字数这种方法在不使用正则表达式的情况下计算单词。sum()、strip() 和 split() 都是 Python 中的内置方法。我们将简要讨论每种方法及其功能。sum() 方法从左到右将项目相加并返回总和。该方法有两个参数:iterable(必需)- 一个字符串、列表、元组等,要加起来。这些应该是数字。start(可选)- 添加到方法的总和或返回值的数字。sum() 的语法:sum(iterable, start)下一个是 strip() 方法,如果没有参数,它返回去除前导和尾随空格的字符串的副本;否则,这将删除参数中定义的字符串。chars(可选)- 指定要从文本左右部分删除的字符串。string.strip() 的语法:string.strip(chars)最后,split() 方法在此方法之前已经讨论过。现在,让我们一起使用这些方法来计算字符串中的单词数。首先,在使用其功能之前,我们需要导入 string,这是一个 Python 内置模块。import string
# initialize string
text = 'Python !! is the be1st $$
programming language @'
# using the sum(), strip(), split() methods
result = sum([i.strip(string.punctuation).isalpha() for i in text.split()])
print("There are " + str(result) + " words.")输出:There are 5 words.四、使用 count() 方法计算 Python 字符串 Python 中的单词数count() 方法是 Python 的内置方法。它接受三个参数并根据给定的子字符串返回出现次数。substring(必需)- 要在字符串中搜索的关键字start(选项)- 搜索开始位置的索引end(选项)- 搜索结束位置的索引注意:在 Python 中索引从 0 开始。count() 的语法:string.count(substring, start, end)此方法与前面的方法不同,因为它不返回在字符串中找到的单词总数,而是返回给定子字符串的出现次数。让我们从下面的例子中看看这个方法是如何工作的:# initialize string
text = "Python: How to count words in string Python"
substring = "Python"
total_occurrences = text.count(substring)
print("There are " + str(total_occurrences) + " occurrences.")输出:There are 2 occurrences.在这种方法中,子串是整个单词、短语、字母还是字符或数字的任意组合都没有关系。总之,你可以根据你的用例选择这些方法中的任何一种。对于空格分隔的单词,我们可以使用简单的方法:函数 split() 或 len()。要过滤文本字符串以计算没有特殊字符的单词,请使用 regex 模块。创建一个计算不包含某些字符的单词的模式。不使用 regex,使用 sum() + strip() + split() 方法组合的替代方法。最后,count() 方法也可用于计算字符串中找到的特定单词。多动手,多练习,多理解,加油!!!觉得不错的话,记得帮我 @小象点个赞哟,祝大家都能学有所获!python相关问题集锦在 Python 中循环遍历字符串的方式有?如何从 Python 中的字符串中提取子字符串?

我要回帖

更多关于 python统计单词出现次数 的文章

 

随机推荐