python oj 1015: 文章检测 | 您所在的位置:网站首页 › 请问你智商多少英语 › python oj 1015: 文章检测 |
1015: 文章检测
题目描述 给你一篇英文文章,请问你里面有多少个空格、数字字符、英文字母和其他字母。 输入 输入多行文字,以 EOF 结束 每一行表示一个文章的段落 输出 输出一行提示信息,具体格式见样例。 样例输入 Python is a good language. We all like it.样例输出 7 spaces, 0 numbers, 32 letters, 3 other characters.提示 以 EOF 结束表示你需要处理一个异常 EOFError 多行文本应该这样读: #!/usr/bin/python3 stopword = '' stri = '' try: for line in iter(input, stopword): stri += line + '\n' except EOFError: pass stri = stri[0:-1] # do something... #1015: 文章检测 import re stopword = 'EOF' stri = '' try: for line in iter(input, stopword): stri += line + '\n' except EOFError: pass stri = stri[0:-1]#不知道这行代码什么意思,但是必须加 #print(stri)#检验了一下 Dp={}#创建了一个字典 Dp['spaces']=len(re.findall(' ',stri)) Dp['numbers']=len(re.findall('[0-9]',stri))#这里用到了正则表达式,想要了解的可以看这个网站,超级全https://deerchao.cn/tutorials/regex/regex.htm Dp['letters']=len(re.findall('[a-zA-Z]',stri)) Dp['other characters']=len(stri) - len(re.findall(' |\d|[A-Za-z]',stri)) print(', '.join("{} {}".format(v, k) for k, v in Dp.items()),end='.') //最后这一句可以好好想想,记住,用到的挺多:join和for和items和print的end |
CopyRight 2018-2019 实验室设备网 版权所有 |