zoukankan      html  css  js  c++  java
  • python练习册 每天一个小程序 第0004题

     1 #-*-coding:utf-8-*- 
     2 __author__ = 'Deen' 
     3 '''
     4 题目描述:任一个英文的纯文本文件,统计其中的单词出现的个数。
     5 参考学习链接:
     6     re  http://www.cnblogs.com/tina-python/p/5508402.html#undefined
     7     collections  http://blog.csdn.net/liufang0001/article/details/54618484
     8 '''
     9 import re,collections
    10 with open('english.txt','r') as fp:
    11     text=fp.read().strip(',')
    12     s=re.compile(r'w+')
    13     words=s.findall(text)
    14     b=list()
    15     dic=collections.defaultdict(lambda :0)
    16     for word in words:
    17         dic[word.lower()] +=1
    18     
    19     print dic
    20 
    21 '''
    22 import collections,re
    23 import sys
    24 def cal(filename = 'english.txt'):
    25     print 'now processing:' + filename + '......'
    26     f = open(filename,'r')
    27     data = f.read()
    28     dic = collections.defaultdict(lambda :0)
    29     data = re.sub(r'[Wd]',' ',data)
    30     data = data.lower()
    31     datalist = data.split(' ')
    32     for item in datalist:
    33         dic[item] += 1
    34     del dic['']
    35     return dic
    36 try:
    37     print sorted(cal().items())
    38 except:
    39     print 'no input file'
    40 '''
  • 相关阅读:
    盛最多水的容器
    除自身以外数组的乘积
    组合总和
    旋转图像
    找到所有数组中消失的数字
    RSA加密、签名机制
    SpringBoot-从新建一个项目看起
    linux下vi命令修改文件及保存的使用方法
    集合
    第一个注解式的SpringMVC项目
  • 原文地址:https://www.cnblogs.com/deen-/p/7147991.html
Copyright © 2011-2022 走看看