Python第四次做業——黃亦楊

設計題1:

設計一個本月份日曆,輸出格式以下:git

要求:
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
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 = 1
count = 0
print("\t2019年4月\n")
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/hyyPython/karomdili/blob/master/Python%E7%AC%AC%E5%9B%9B%E6%AC%A1%E4%BD%9C%E4%B8%9A%E5%85%B6%E4%B8%80#L1設計

 

設計題2:

1.參考「三國演義」詞頻統計程序,實現對紅樓夢出場人物的頻次統計。
2.(可選)
將紅樓夢出場人物的頻次統計結果用詞雲顯示。code

import jieba
excludes = {"什麼","咱們","那裏","一個","現在","說道","起來","知道","姑娘","這裏","出來","他們","衆人","本身","大家",
            "太太","只見","怎麼","奶奶","兩個","沒有","不是","不知","一面","這個","這個","聽見","這樣","進來","我們",
            "告訴","就是","東西","平兒","回來", "只是","老爺","你們","只得","姨媽","姐姐","賈政","只管","晴雯",
            "賈珍","這麼","說話","一回","那邊","這話","外頭","打發","丫頭","今日","銀子","幾個","答應","這些","不敢",
            "出去","因此","不過","的話","很差","鴛鴦","一時","不能","過來","內心","二人","天然","今兒","罷了","還有",
            "屋裏","如此","那些","據說","丫頭","如何","問道","看見","紫鵑","妹妹","人家","不用","媳婦","香菱", "原來",
            "一聲","一句","過去","幫忙","連忙","心中","方纔","仍是","婆子"
           }
txt = open("F:\Python\紅樓夢.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 == "混世魔王" or word == "絳洞花王" or word == "濁玉" or word == "遮天大王" or word == "檻內人": 
        rword = "賈寶玉"
    elif word == "黛玉" or word == "瀟湘妃子" or word == "林妹妹" or word == "林姑娘" or word == "顰兒" or word == "顰顰":
        rword = "林黛玉"
    elif word == "寶釵" or 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 == "老太太" or word == "史太君" or word == "老祖宗":
        rword = "賈母"
    elif word == "枕霞舊友" or word == "史大姑娘" or word == "雲妹妹"  or word == "湘雲":
        rword = "史湘雲"
    elif word == "探春" or word == "三姑娘" or word == "蕉下客":
        rword = "賈探春" 
    elif word == "劉姥姥" or word == "母蝗蟲":
        rword = "劉姥姥"  
    elif word == "璉二爺" or word == "賈璉道":
        rword = "賈璉"
    elif word == "王夫人道":
        rword = "王夫人"    
    elif word =="惜春"or word == "賈惜春":
        rword ="賈惜春"
    elif word =="迎春"or word == "賈迎春":
        rword ="賈迎春"
    elif word == "襲人" or 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列排序
items.sort(key=lambda x:x[1], reverse=True)

for i in range(10): #出現的詞頻統計
    word, count = items[i] #將鍵和值分別賦予列表word和countf
    print ("{0:<10}{1:>5}".format(word, count))#0:<10左對齊,寬度10,」>5"右對齊

碼雲地址:https://gitee.com/hyyPython/karomdili/commit/9d5a6093f0462cc20ad6aeff049f617aad118a08orm

相關文章
相關標籤/搜索