第4次Python作业——沈朝铨

设计题1:

设计一个本月份日历,输出格式如下:
7fe1d8a9ea403301e3d8e953b3b2748.png

要求:
1.初始化start_day,end_day两个日期
from datetime import datetime
start_day=datetime(2019,4,1)
end_day=datetime(2019,4,30)
其它时间数据生成要用datetime或date模块的方法编程实现
2.不能使用calendar模块生成

from datetime import datetime
from datetime import *
start_day = datetime(2019, 4, 1)
end_day = datetime(2019, 4, 30)
a = end_day-start_day
amount = a.days + 1
first = start_day.isoweekday()
k = 0
count = 0
print("\t\t\t2019年4月")
print("星期日 星期一 星期二 星期三 星期四 星期五 星期六")
while k<=first:
    k+=1
    count+=1
    print("\t", end="")
 
day = 1
while day <= amount:
    print(day, end="\t")
    day += 1
    count += 1
    if (count % 7 == 0):
        print("\n")

码云地址:https://gitee.com/Scq1001/codes/niuodaezmbqxwt591kvj778

设计题2:

1.参考“三国演义”词频统计程序,实现对红楼梦出场人物的频次统计。
2.(可选)
将红楼梦出场人物的频次统计结果用词云显示。

import jieba
excludes = {"一个","一回","袭人","银子","王夫人","东西","姑娘","什么"
            ,"我们","贾母","那里","说道","众人","一面","这个","不知","没有","两个"
            ,"奶奶","怎么","只见","自己","他们","出来","这里","起来","知道","如今","你们",
            "一个","我们","那里","如今","说道","知道","起来","姑娘","这里","出来",
            "他们","众人","自己","一面","只见","太太","两个","没有","不是","不知",
            "这个","听见","这样","进来","咱们","告诉","怎么","就是","东西","回来","只是",
            "老爷","大家","只得","丫头","这些","不敢","出去","所以","的话","不好","姐姐"}
txt = open(r"C:\python 1\红楼梦.txt", "r", encoding='utf-8').read()
words  = jieba.lcut(txt)#精确模式的分词函数,返回一个列表数据类型
#print(type(words)) #words的数据类型
counts = {}  #定义一个字典
for word in words:
    if len(word) == 1:
        continue
    elif word == "宝玉" or word == "宝二爷" or word == "怡红公子" or word == "绛洞花主" or word == "无事忙" or 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=="凤辣子"or word=="凤哥儿"or word=="凤丫头" or word=="凤姐儿" or word == "熙凤道":
        rword = "王熙凤"
    elif word == "贾母" or word == "老太太":
        rword = "贾母"
    elif word == "贾探春" or word == "探春":
        rword = "探春"
    elif word == "秦可卿" or word == "可卿":
        rword = "可卿"
    elif word == "司棋" or word == "司棋道":
        rword = "司棋"
    else:
        rword = word
    counts[rword] = counts.get(rword,0) + 1 #词汇加入字典
for word in excludes:
    del(counts[word])  #从字典中删除无用词
items = list(counts.items())#字典转换为列表

#lambda是一个隐函数,是固定写法,以下命令的意思就是按照记录的第2列排序
"""x表示列表中的一个元素,x只是临时起的一个名字,
你可以使用任意的名字"""
items.sort(key=lambda x:x[1], reverse=True)

for i in range(7): #出现的词频统计
    word, count = items[i] #将键和值分别赋予列表word和countf
    print ("{0:<10}{1:>5}".format(word, count))#0:<10左对齐,宽度10,”>5"右对

码云地址:https://gitee.com/Scq1001/codes/ic93ywptd2z5ghv6bom1l18

猜你喜欢

转载自www.cnblogs.com/vfdsg/p/10838103.html