python怎么统计一句英语的单词数量并输出?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python怎么统计一句英语的单词数量并输出?相关的知识,希望对你有一定的参考价值。
python怎么统计一句英语的单词数量并输出?
题主你好,
代码及测试截图如下:
说明: 上图红框处的result可不写, 只是为了看一下分隔结果是否正确.
希望可以帮到题主, 欢迎追问.
需要统计若干段文字(英文)中的不同单词数量。 如果不同的单词数量不超过10个,则将所有单词输出(按字母顺序),否则输出前10个单词。注1:单词之间以空格(1个或多个空格)为间隔。 注2:忽略空行或者空格行。 注3:单词大小写敏感,即’word’与’WORD’是两个不同的单词 。输入说明 若干行英文,最后以!!!为结束。输出说明 不同单词数量。 然后输出前10个单词(按字母顺序),如果所有单词不超过10个,则将所有的单词输出。输入样例 Failure is probably the fortification in your pole It is like a peek your wallet as the thief when you are thinking how to spend several hard-won lepta when you Are wondering whether new money it has laid background Because of you, then at the heart of the most lax alert and most low awareness and left it godsend failed !!! 输出样例 49 Are Because Failure It a alert and are as at两种实现方法:"""str=""while True:words=input()if words=="!!!!!":breakstr=str+" "+wordsstr_list=[]i=0for s in str.split():if s not in str_list:str_list.append(s)i+=1print(i)str_list.sort()if len(str_list)<10:for s in str_list:print(s)else:for s in range(10):print(str_list[s])"""str_list=[]n=0while True:str=input()if str=="!!!!!":breakfor i
提问这是啥啊
回答这篇文章主要介绍了Python实现统计英文单词个数及字符串分割方法,本文分别给出代码实例,需要的朋友可以参考下字符串分割代码如下:str="a|and|hello|||ab"alist = str.split(\'|\')print alist结果复制代码代码如下:str="a hello这里换成5个空格world这里换成3个空格"alist=str.split(\' \')print alist统计英文单词的个数的python代码代码如下:# -*- coding: utf-8 -*-import os,sysinfo = os.getcwd() #获取当前文件名称fin = open(u\'c:/a.txt\')info = fin.read()alist = info.split(\' \') # 将文章按照空格划分开fout = open(u\'c:/count.txt\', \'w\')fout.write(\'\\n\'.join(alist)) # 可以通过文本文件的行号同样看到效果##fout.write(\'%s\' % alist)fout.close()allen = len(alist) # 总的单词数nulen = alist.count(\'\') # 空格的数量print "words\' number is",allenprint "null number is",nulenprint "poor words number is", allen-nulen # 实际的单词数目fin.close()————————————————版权声明:本文为CSDN博主「逆光时」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。原文链接:https://blog.csdn.net/weixin_36304087/article/details/113682810
可以的
提问就是求Python is an 这句话的单词数量输出结果是3,用spilt的方法
你这一串代码我完全看不懂啊
回答Python,分割连续的字符串一般使用spilt方法如:str.splitlines([keepends])
go语言小练习——给定英语文章统计单词数量
给定一篇英语文章,要求统计出所有单词的个数,并按一定次序输出。思路是利用go语言的map类型,以每个单词作为关键字存储数量信息,代码实现如下:
1 package main 2 3 import ( 4 "fmt" 5 "sort" 6 ) 7 8 func wordCounterV1(str string) { 9 /*定义变量*/ 10 stringSlice := str[:] 11 temp := str[:] 12 wordStatistic := make(map[string]int) 13 14 /*把所有出现的单词放入map中*/ 15 j := 0 16 for i := 0; i < len(stringSlice); i++ { 17 if !((stringSlice[i] >= 65 && stringSlice[i] <= 90) || (stringSlice[i] >= 97 && stringSlice[i] <= 122)) { 18 temp = str[j:i] 19 if len(temp) != 0 { 20 wordStatistic[temp]++ 21 } 22 j = i + 1 23 } 24 } 25 26 /*把首字母为大写的单词转换为小写;去除无效字符*/ 27 for i := range wordStatistic { 28 if len(i) > 1 { 29 if (i[0] >= 65 && i[0] <= 90) && (i[1] <= 65 || i[1] >= 90) { 30 strTemp := make([]byte, len(i), len(i)) 31 copy(strTemp, i) 32 strTemp[0] += 32 33 wordStatistic[string(strTemp)] += wordStatistic[i] 34 delete(wordStatistic, i) 35 } 36 } else { 37 if i[0] != ‘a‘ && i[0] != ‘A‘ { 38 delete(wordStatistic, i) 39 } else if i[0] == ‘A‘ { 40 wordStatistic["a"] += wordStatistic[i] 41 delete(wordStatistic, i) 42 } 43 } 44 45 } 46 47 /*把map的关键字映射到string切片进行排序*/ 48 sortSlice := make([]string, 0, len(wordStatistic)) 49 for i := range wordStatistic { 50 sortSlice = append(sortSlice, i) 51 } 52 sort.Strings(sortSlice) 53 54 /*输出结果*/ 55 for _, v := range sortSlice { 56 fmt.Printf("%s:%d ", v, wordStatistic[v]) 57 } 58 fmt.Printf("word count:%d ", len(wordStatistic)) 59 }
主函数随便输入一篇英语文章
func main() {
str := ` There are moments in life when you miss someone so much that you just want to pick them from your dreams and hug them for real! Dream what you want to dream;go where you want to go;be what you want to be,because you have only one life and one chance to do all the things you want to do. May you have enough happiness to make you sweet,enough trials to make you strong,enough sorrow to keep you human,enough hope to make you happy? Always put yourself in others’shoes.If you feel that it hurts you,it probably hurts the other person, too. The happiest of people don’t necessarily have the best of everything;they just make the most of everything that comes along their way.Happiness lies for those who cry,those who hurt, those who have searched,and those who have tried,for only they can appreciate the importance of people who have touched their lives.Love begins with a smile,grows with a kiss and ends with a tear.The brightest future will always be based on a forgotten past, you can’t go on well in life until you let go of your past failures and heartaches. When you were born,you were crying and everyone around you was smiling. Live your life so that when you die,you‘re the one who is smiling and everyone around you is crying. Please send this message to those people who mean something to you, to those who have touched your life in one way or another,to those who make you smile when you really need it,to those that make you see the brighter side of things when you are really down,to those who you want to let them know that you appreciate their friendship.And if you don’t, don’t worry,nothing bad will happen to you,you will just miss out on the opportunity to brighten someone’s day with this message.`
//调用功能 wordCounterV1(str) }
编译后终端输出结果:
C:Users24213go project>cd srcgithub.comgo-studylesson6practice1 C:Users24213go projectsrcgithub.comgo-studylesson6practice1>go build C:Users24213go projectsrcgithub.comgo-studylesson6practice1>practice1 a:4 all:1 along:1 always:2 and:8 another:1 appreciate:2 are:2 around:2 bad:1 based:1 be:3 because:1 begins:1 best:1 born:1 brighten:1 brighter:1 brightest:1 can:2 chance:1 comes:1 cry:1 crying:2 day:1 die:1 do:2 don:3 down:1 dream:2 dreams:1 ends:1 enough:4 everyone:2 everything:2 failures:1 feel:1 for:3 forgotten:1 friendship:1 from:1 future:1 go:4 grows:1 happen:1 happiest:1 happiness:2 happy:1 have:7 heartaches:1 hope:1 hug:1 human:1 hurt:1 hurts:2 if:2 importance:1 in:4 is:2 it:3 just:3 keep:1 kiss:1 know:1 let:2 lies:1 life:5 live:1 lives:1 love:1 make:6 may:1 mean:1 message:2 miss:2 moments:1 most:1 much:1 necessarily:1 need:1 nothing:1 of:6 on:3 one:4 only:2 opportunity:1 or:1 other:1 others:1 out:1 past:2 people:3 person:1 pick:1 please:1 probably:1 put:1 re:1 real:1 really:2 searched:1 see:1 send:1 shoes:1 side:1 smile:2 smiling:2 so:2 someone:2 something:1 sorrow:1 strong:1 sweet:1 tear:1 that:6 the:10 their:3 them:3 there:1 they:2 things:2 this:2 those:9 to:19 too:1 touched:2 trials:1 tried:1 until:1 want:6 was:1 way:2 well:1 were:2 what:2 when:5 where:1 who:10 will:3 with:4 worry:1 you:32 your:4 yourself:1 word count:144
以上是关于python怎么统计一句英语的单词数量并输出?的主要内容,如果未能解决你的问题,请参考以下文章
Python里,输入一个英文句子,统计并输出单词个数,怎么弄啊?