第1.4題:統(tǒng)計文本中單詞出現的個數

題目來自:Python 練習冊章喉。任一英文的純文本文件,統(tǒng)計其中的單詞出現個數身坐。查看更多見:iii.run


鋪墊工作

這一期的鋪墊工作比較多秸脱,所以單獨寫了一篇文章,詳見 Python正則表達式

正文部分

題目內容

任一個英文的純文本文件部蛇,統(tǒng)計其中的單詞出現的個數摊唇。
參考英文:

If you are looking for someone you can pour out your love to, let me suggest the empowered woman. The empowered woman knows what she wants, knows how to get it, knows how to live fully, and she knows how to love you back without needing anyone’s approval or recognition. An empowered woman is unarguably one of the most magnificent beings you will ever come in contact with. Read on and find 10 reason why you should absolutely love and embrace the empowered women in your life! . 

1. She knows how to love you in returnIt is difficult to give what you don’t have. It is impossible to love someone and feel fulfilled when they can’t love you in return because they don’t love themselves. This will never happen to you when you love an empowered woman. She loves herself (not in a narcissistic manner). In turn, she appreciates who you are and loves you in return. She will love you just like you deserve to be loved. 

2. She will inspire youWhen life puts you down and you are at the end of your rope, the empowered woman will be there to see you through. Her drive, enthusiasm and (at times) hopeless optimism will inspire you to carry on despite the obstacles you face. 

3. She is not afraid of failureWhile many out there are thoroughly terrified of failure, the empowered woman understands that failures are simply stepping stones in life. How can you not love someone that is thoroughly unafraid to try, fail, and give it a shot all over again?! 


4. She is all about the legacyWhile most people are focused on the car, the house, the job, and corner office; the empowered woman is focused on leaving a legacy that will inspire others and change the world. The empowered woman is focused on empowering others to maximize their potential and fulfill their purpose. She is all about inspiring others to look beyond themselves and live a life of service to others.


 5. She can laugh at her mistakes……and learn from them as well! She understands mistakes are part of the journey. The empowered woman can laugh and learn from her mistakes to ensure they never happen again. 


6. She can be vulnerableThe empowered woman understands there is no debt in relationships without vulnerability. Although she is emotionally strong, she is willing to laugh and cry with you because all of these emotions are an essential part of life. 


7. She can speak her mindWhile everyone else is too concerned with what others may think or say, the empowered woman is not afraid to speak her mind. She understands that her value comes from within, not from what others say or think about her. 


8. She knows when to remain quietShe lives by Abe Lincoln’s words, “Better to remain silent and be thought a fool, than to speak out and remove all doubt.” 


9. She knows how to have funWhether it is at the symphony or at a ball game, the empowered woman understands life is made up of experiences with people – not the places you go. She is able to live in the moment and enjoy it fully without being concerned for the future. After all, who’s got a guaranteed future?


 10. She is not afraid of changeWhile most people rather continue on living unfulfilled lives as long as their comfort zone remains intact, the empowered woman is all about embracing change. She understands growth cannot happen without change. She understands that change is the gift life offers you to choose your destiny. Therefore, she is not afraid of change because it is her stepping stone towards success.

下載鏈接
將文件下載到python的工作路徑里去,如果不知道哪里是工作路徑涯鲁,輸入

import os
#獲取當前工作目錄
os.getcwd()
#更改當前工作目錄
os.chdir('d:\')
os.getcwd()

參考代碼

每一步我都盡量附帶上了解釋

# coding=utf-8
from collections import defaultdict
import re
# 替換除了n't這類連字符外的所有非單詞字符和數字字符
def replace(s):
    if s.group(1) == 'n\'t':
        return s.group(1)
    return ' '
def cal(filename='203305485.txt'):
    # 使用lambda來定義簡單的函數
    dic = defaultdict(lambda: 0)#dic = defaultdict(int)也可以
    with open(filename, 'r') as f:
        data = f.read()
    # 全部變?yōu)樾懽帜?    data = data.lower()
    # 替換除了n't這類連字符外的所有非單詞字符和數字字符
    data = re.sub(r'(n[\']t)|([\W\d])', replace, data)
    datalist = re.split(r'[\s\n]+', data)
    for item in datalist:
        dic[item] += 1
    del dic['']
    return dic

if __name__ == '__main__':
    dic = cal()
    for key, val in dic.items():
        print('%15s  ---->   %3s' % (key,val))

運行結果如下:


結果

增加排序函數

代碼有參考 《利用python進行數據分析》

def top_counts(dic, n=10):
    value_key_pairs = [(count, tz) for tz, count in dic.items()]
    value_key_pairs.sort()
    return value_key_pairs[-n:]
top_counts(dic)

運行結果如下:

結果

可以看出巷查,人們最喜歡用的詞是定冠詞the,下來是介詞to.......


補充

最近發(fā)現collections模塊的Counter類 抹腿,
導入語句是:from collections import Counter岛请,作用是:定義一個list數組,求數組中每個元素出現的次數
修改之后代碼量要少很多幢踏,而且可以直接排列好順序~

# coding=utf-8
import re
from collections import Counter

def cal(filename='203305485.txt'):
    with open(filename, 'r') as f:
        data = f.read()
    data = data.lower()
    # 替換除了n't這類連字符外的所有非單詞字符和數字字符
    datalist = re.split(r'[\s\n]+', data)
    return Counter(datalist).most_common()
    
if __name__ == '__main__':
    dic = cal()
    for i  in range(len(dic)):
        print('%15s  ---->   %3s' % (dic[i][0],dic[i][1]))

代碼看起來行云流水髓需,舒服多了许师。當然結論是一樣的房蝉,人們還是比較喜歡說 the , you~

修改后

以上~

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末僚匆,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子搭幻,更是在濱河造成了極大的恐慌咧擂,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,589評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件檀蹋,死亡現場離奇詭異松申,居然都是意外死亡,警方通過查閱死者的電腦和手機俯逾,發(fā)現死者居然都...
    沈念sama閱讀 93,615評論 3 396
  • 文/潘曉璐 我一進店門贸桶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人桌肴,你說我怎么就攤上這事皇筛。” “怎么了坠七?”我有些...
    開封第一講書人閱讀 165,933評論 0 356
  • 文/不壞的土叔 我叫張陵水醋,是天一觀的道長。 經常有香客問我彪置,道長拄踪,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,976評論 1 295
  • 正文 為了忘掉前任拳魁,我火速辦了婚禮惶桐,結果婚禮上,老公的妹妹穿的比我還像新娘潘懊。我一直安慰自己耀盗,他們只是感情好,可當我...
    茶點故事閱讀 67,999評論 6 393
  • 文/花漫 我一把揭開白布卦尊。 她就那樣靜靜地躺著叛拷,像睡著了一般。 火紅的嫁衣襯著肌膚如雪岂却。 梳的紋絲不亂的頭發(fā)上忿薇,一...
    開封第一講書人閱讀 51,775評論 1 307
  • 那天,我揣著相機與錄音躏哩,去河邊找鬼署浩。 笑死,一個胖子當著我的面吹牛扫尺,可吹牛的內容都是我干的筋栋。 我是一名探鬼主播,決...
    沈念sama閱讀 40,474評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼正驻,長吁一口氣:“原來是場噩夢啊……” “哼弊攘!你這毒婦竟也來了抢腐?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,359評論 0 276
  • 序言:老撾萬榮一對情侶失蹤襟交,失蹤者是張志新(化名)和其女友劉穎迈倍,沒想到半個月后,有當地人在樹林里發(fā)現了一具尸體捣域,經...
    沈念sama閱讀 45,854評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡啼染,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,007評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現自己被綠了焕梅。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片迹鹅。...
    茶點故事閱讀 40,146評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖贞言,靈堂內的尸體忽然破棺而出徒欣,到底是詐尸還是另有隱情,我是刑警寧澤蜗字,帶...
    沈念sama閱讀 35,826評論 5 346
  • 正文 年R本政府宣布打肝,位于F島的核電站,受9級特大地震影響挪捕,放射性物質發(fā)生泄漏粗梭。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,484評論 3 331
  • 文/蒙蒙 一级零、第九天 我趴在偏房一處隱蔽的房頂上張望断医。 院中可真熱鬧,春花似錦奏纪、人聲如沸鉴嗤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,029評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽醉锅。三九已至,卻和暖如春发绢,著一層夾襖步出監(jiān)牢的瞬間硬耍,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,153評論 1 272
  • 我被黑心中介騙來泰國打工边酒, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留经柴,地道東北人。 一個月前我還...
    沈念sama閱讀 48,420評論 3 373
  • 正文 我出身青樓墩朦,卻偏偏與公主長得像坯认,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,107評論 2 356

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,190評論 25 707
  • 是這個節(jié)氣了。寒露已過荆隘,又過重陽。清早下了一場冷雨赴背,風強勢地沖進門椰拒,直直襲來,叫醒鬧鈴凰荚,也涼醒了我燃观。 洗漱起床,之...
    七點水閱讀 351評論 0 0
  • 最近你的朋友圈是不是被這張圖刷屏了。 又或者是這張圖刷屏了到涂。 沒有原因脊框,因為它實在是太火了,首日票房就過億了践啄。 本...
    大浪2018閱讀 662評論 1 7
  • 有個人很經常問我一些問題浇雹,讓我煩躁又頭疼。 比如昨天屿讽,問我奇葩說的題目:閨蜜約你去撕小三昭灵,你去還是不去? 我說:撕...
    陳煒杭閱讀 344評論 0 0
  • 昨天看到古典老師的新書《躍遷》在搞宣傳預熱,下載了搶讀版提前拜讀了一下诵棵。 雖然是個試讀版抠蚣,內容卻不水,讀來暢快淋漓...
    清心瀾意閱讀 1,970評論 2 5