1.初始化start_day,end_day两个日期
from datetime import datetimestart_day=datetime(2019,4,1)end_day=datetime(2019,4,30)其它时间数据生成要用datetime或date模块的方法编程实现2.不能使用calendar模块生成from datetime import datetime
from datetime import timedeltafrom datetime import *start_day = datetime(2019, 4, 1)end_day = datetime(2019, 4, 30)a=end_day-start_dayamount = a.days + 1first=start_day.isoweekday()k = 1count=0print("\t\t\t2019年4月")print("星期一\t星期二\t星期三\t星期四\t星期五\t星期六\t星期日")while k < first: k=k+1 print("\t", end="")p = 1while p <= amount: print(p, "\t", end="") p =p+1 count =count+1 if (count % 7 == 0): print("\n")1.参考“三国演义”词频统计程序,实现对红楼梦出场人物的频次统计。
2.(可选)将红楼梦出场人物的频次统计结果用词云显示。import jiebaexcludes = {"什么","一个","我们","那里","你们","如今","说道","知道","起来","这里","出来","他们","众人","自己","奶奶","一面","只见","怎么","姑娘","两个","没有","不是","不知","这个","听见","这样","进来","这是", "告诉","就是","咱们","东西","回来","只是","大家","老爷","只得","丫头","这些","不敢","出去","所以","不过","的话","不好","姐姐"}txt = open("红楼梦.txt", "r", encoding='utf8').read() words = jieba.lcut(txt)counts = {} for word in words: if len(word) == 1: continueelif (word == "宝玉" or word == "宝玉道"or word == "宝二爷" or word == "混世魔王"or word == "怡红公子"or word == "绛洞花主" or word == "无事忙"or word == "遮天大王"or word == "富贵闲人"or word =="贾宝玉"):rword = "贾宝玉" elif word == "黛玉" or word == "黛玉道"or word =="林黛玉": rword = "林黛玉" elif word == "宝钗" or word == "宝钗道"or word =="薛宝钗": rword = "薛宝钗" elif word == "姨太太" or word == "薛姨妈": rword = "薛姨妈"elif word == "老祖宗" or word == "老太太"or word == "史太君"or word =="贾母": rword = "贾母" elif word == "太太" or word == "二太太": rword = "王夫人" elif word == "熙凤" or word == "熙凤道"or word == "凤姐"or word == "凤姐儿"or word == "王熙凤": rword = "王熙凤" elif word == "平儿" or word == "袭人"or word == "小平": rword = "平儿" elif word == "探春" or word == "探春道": rword = "贾探春" elif word == "晴雯" or word == "勇晴雯"or word == "芙蓉仙子"or word == "病西施": rword = "晴雯" else: rword = wordcounts[rword] = counts.get(rword, 0) + 1 for word in excludes: del (counts[word]) items = list(counts.items()) items.sort(key=lambda x: x[1], reverse=True) for i in range(10): word, count = items[i] print("{0:<10}{1:>7}".format(word, count))