Python 统计单词数

您所在的位置:网站首页 python输出单词个数的函数 Python 统计单词数

Python 统计单词数

2023-05-31 21:43| 来源: 网络整理| 查看: 265

 统计单词的数量 描述 输入一个英文句子,以回车符结束,单词间以空格分隔,标点符号后跟至少一个空格, 统计并输出单词的个数(仅统计单词,数字按单词计,不计算标点符号,重复单词出现几次就统计几次)。‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

输入 一个英文句子‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

输出 一个整数,单词数量‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‭‬‫‬

输入输出示例 输入 输出 示例 1 Life is short, you need Python! 6

string库: string.ascii_letters 大小写英文字母 string.ascii_lowercase 小写英文字母 string.ascii_uppercase 大写英文字母 string.digits 数字0-9 这些生成的都是字符串

改进:(不知道有没有漏洞)  

import string x=input() for i in string.punctuation: x=x.replace(i,'') print(x) ls=x.split() print(len(ls))

#1

x=input() flag=0 b=0 for i in x: if not(i.isalpha() or i.isdigit()): b=0 elif b==0 : flag+=1 b=1 print(flag)

#2:

from string import * x=input() if x[0] == ' ': count =0 else: count =1 for i in range(len(x)): if x[i] ==' 'and x[i+1] in ascii_letters+digits: count+=1 print(count)



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3