天天看點

複合資料類型,英文詞頻統計

1 ###清單###
 2 list = ['lin', 'kai', 14, 29, 'xan']
 3 
 4 # 增
 5 list.append(22)  # 增加到清單尾
 6 list.extend(['extend'])  # 添加指定清單的所有元素
 7 list.insert(2, '插入')  # 在指定位置插入一個元素
 8 print(list)
 9 
10 # 删
11 list.remove('xan')  # 删除清單中值為'xan'的元素
12 list.pop()  # 删除最後一個元素
13 list.pop(1)  # 删除指定位置元素
14 del list[2:4]  # 删除從2到4的元素
15 list.clear()  # 删除所有項
16 print(list)
17 
18 # 改
19 list = ['2', 'lin', 'kai', 14, 29, 'xan']
20 list[0] = 'ig'
21 print(list)
22 
23 # 查
24 print(list.index(14))  # 傳回第一個值為x的元素的索引
25 
26 # 周遊
27 for i in list:
28     print(i)
29 
30 
31 
32 ###元祖###
33 tuple = ('Google', 'Baidu', 'oo', 'uu', 15, 26)  # 元祖
34 
35 # 元組中的元素值是不允許修改的,但我們可以對元組進行連接配接組合
36 tup1 = (1, 2, 3)
37 tup2 = ('a', 'b', 'c')
38 
39 # 建立新的元組
40 tup3 = tup1 + tup2
41 print(tup3)
42 
43 # 通路元祖
44 print("tup1[0]: ", tup1[0])
45 
46 # 周遊
47 for i in tup1:
48     print(i)
49 
50 
51 
52 ###字典###
53 dict = {'a': '1', 'b': '2', 'c': '3'}  # 字典
54 
55 # 删
56 del dict['b']  # 删除鍵 'b'
57 dict.clear()  # 清空字典
58 del dict  # 删除字典
59 
60 dict = {'a': '1', 'b': '2', 'c': '3'}
61 # 改
62 dict['a'] = '9'
63 # 增
64 dict['d'] = '4'
65 # 查
66 print(dict['a'])
67 
68 # 周遊
69 for i, j in dict.items():
70     print(i, ":\t", j)
71 
72 
73 
74 ###集合###
75 set1 = {1, 2, 3}  # 集合
76 set2 = set((1, 2, 3))  # 也是集合
77 print(set1)
78 print(set2)
79 
80 # 增
81 set1.add(4)
82 print(set1)
83 
84 # 删
85 set2.remove(1)  # 移除值為1的元素,如果不存在則會發生錯誤
86 set2.discard(1)  # 移除值為1的元素,如果不存在不會發生錯誤
87 set2.pop()  # 随機删除一個元素
88 set2.clear()
89 print(set2)
90 
91 # 查
92 print(1 in set1)  # 存在則傳回True
93 
94 # 集合無序,不能修改指定的元素
95 # 周遊
96 for num in set1:
97     print(num)      

總結清單,元組,字典,集合的聯系與差別。參考以下幾個方面:

  • 括号
  • 有序無序
  • 可變不可變
  • 重複不可重複
  • 存儲與查找方式
清單   元祖  字典 集合
 []  ()  {} {}
 有序  無序,自動正序 無序
 可變  不可變 可變
 可以 不可以
 值  鍵值對(鍵不能重複) 鍵(不能重複)

詞頻統計

  • 1.下載下傳一長篇小說,存成utf-8編碼的文本檔案 file

    2.通過檔案讀取字元串 str

    3.對文本進行預處理

    4.分解提取單詞 list

    5.單詞計數字典 set , dict

    6.按詞頻排序 list.sort(key=lambda),turple

    7.排除文法型詞彙,代詞、冠詞、連詞等無語義詞

    • 自定義停用詞表
    • 或用stops.txt

          8.輸出TOP(50)

      

複合資料類型,英文詞頻統計

      排序好的單詞清單word儲存成csv檔案

      

複合資料類型,英文詞頻統計
1 import re
 2 import pandas
 3 from stop_words import get_stop_words
 4 
 5 stop_words = get_stop_words('english')
 6 
 7 file = open("../第三次作業/Howl’s Moving Castle.txt", "r+", encoding='UTF-8')
 8 str = file.read()
 9 
10 str = re.sub('[\r\n\t,!?:;“‘’.\"]', '', str)
11 words = str.split(" ")
12 
13 single = []  # 分詞數組
14 excluding_words = []  # 排除的單詞
15 quantity = []  # 單詞次數
16 
17 for word in words:
18     if (word not in single and word not in stop_words):
19         single.append(word)
20 
21 tmp = single.copy()
22 while tmp:
23     # print(tmp[0], "出現次數:", words.count(tmp[0]), end="     ")
24     quantity.append(words.count(tmp[0]))
25     tmp.pop(0)
26 
27 dic = dict(zip(single, quantity))  # 合成字典
28 dic = sorted(dic.items(), key=lambda item: item[1], reverse=True)  # 排序
29 
30 for i in range(50):
31     print(dic[i][0], " : ", dic[i][1])
32 
33 pandas.DataFrame(data=dic).to_csv('fenci.csv', encoding='utf-8')
34 
35 print(stop_words)
36 
37 file.close()      

    9.可視化:詞雲

複合資料類型,英文詞頻統計