1. 程式人生 > >組合數據類型練習、英語詞頻統計

組合數據類型練習、英語詞頻統計

split ems 分析字符串 練習 數字 col 字符 內存 ssi

1.字典實例:建立學生學號成績字典,做增刪改查遍歷操作。
dict={02:59,"03":70,"04":96,"05":85} dict print(dict.get(04)) dict.pop(03) print(dict.items()) dict[01]=60 print(dict.items()) dict["06"]=74 print(dict.items()) print("查找:") print(dict.get(05,不存在)) print(dict.get(09,不存在)) print(dict.items()) print("遍歷") for i in dict:
print(i)
技術分享


2.列表,元組,字典,集合的遍歷。
lis=["10","09","08","07"] tu=("10","09","08","07","06") dic={"10":"63","09":"75","08":"66","07":"92"} s=set(lis) for i in lis: print(i) for i in tu: print(i) for i in dic: print({0:<}:{1:>}.format(i,dic[i])) for i in s: print(i)
技術分享



總結列表,元組,字典,集合的聯系與區別。

①列表,元組是有順序的,而字典和集合是沒順序的。

②列表是可變對象,可以有增刪改操作,而元組是只讀的,不能修改。

③字典使存儲鍵值對數據,鍵是不可變的對象。插入和查找速度快,不會隨著鍵的增加而變慢,需要占用大量的內存。

④字典是用空間換取時間的一種方法。集合是一個無序不重復元素集, 基本功能包括關系測試和消除重復元素。

英文詞頻統計實例
  1. 待分析字符串
  2. 分解提取單詞
    1. 大小寫 txt.lower()
    2. 分隔符‘.,:;?!-_’
    3. 單詞列表
  3. 單詞計數字典

s=‘‘‘4... 3... 2... 1... no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire even if you say I have been the world wide I‘ll take you where surely you have never been all right in the fight I‘m OK... come on come on hey do you feel the night is breathable look at this town which is unbelievable no other places like that in the world world world 1 2 3 4 no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire turning to the left easy chicks and red lights and to the right crazy music everywhere all right in the fight I‘m OK... come on come on hey do you feel the night is breathable look at this town which is unbelievable no other places like that in the world world world 1 2 3 4 no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire come on 1 2 3 4 all right crossing the line Tokyo is on fire hey do you feel the night is breathable look at this town which is unbelievable no other places like that in the world world world 1 2 3 4 no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire no one sleep in Tokyo all right crossing the line no one quit the radio Tokyo is on fire
‘‘‘ print(短文中各個短語出現的次數:) s=s.lower() print(全部轉為小寫:) print(s) for i in ,.: s=s.replace(i, ) words=s.split( ) d = {} words.sort() disc = set(words) for i in disc: d[i] = 0 for i in words: d[i] = d[i]+1 n = d.items() print(單詞計數:) print(n)

技術分享 技術分享 技術分享

組合數據類型練習、英語詞頻統計