做業博客要求:app
1.列表,元組,字典,集合分別如何增刪改查及遍歷。函數
(1)列表工具
list = ['a','b','hello',1]
#第一在列表後方添加數據 第二爲在對應的下邊插入數據 list.append(2) list.insert(0,'0') print(list)
經過[]來建立列表,可經過索引(index)來獲取列表中的元素和修改元素;append() 方法向列表的最後添加一個元素,insert()向列表的指定位置插入一個元素,extend()使用新的序列來擴展當前序列,須要一個序列做爲參數,它會將該序列中的元素添加到當前列表中;經過pop() 根據索引刪除並返回被刪除的元素;通常經過for循環來遍歷列表,如for s in stus :print(s)形式。編碼
(2)元祖spa
使用()來建立元組,它的操做的方式基本上和列表是一致的。但元組是不可變的序列,不能嘗試爲元組中的元素從新賦值code
(3)字典blog
使用 {} 來建立字典,每個元素都是鍵值對,鍵不重複,值能夠重複。排序
(4)集合索引
使用 {} 或set() 函數來建立集合,操做與字典相似,但只包含鍵,而沒有對應的值,包含的數據不重複。能夠經過set()來將序列和字典轉換爲集合。utf-8
2.總結列表,元組,字典,集合的聯繫與區別。參考如下幾個方面:
(1)列表用[]表示,有序,可變,可重複,元素以值的方式存儲爲值,可經過索引查找,如mylist[1]
(2)元組用()表示,有序,不可變,可重複,元素以值的方式存儲爲值,可經過索引查找,如tuple[0]
(3)字典用{}表示,無序,鍵不可重複,值能夠重複,元素以鍵值對的方式存儲爲值,通常經過鍵查找,如dist['key']
(4)集合用{}表示,無序,可變,不可重複,元素以值的方式存儲爲值,能夠經過set()來將序列和字典轉換爲集合。
3.詞頻統計
1.下載一長篇小說,存成utf-8編碼的文本文件 file
2.經過文件讀取字符串 str
3.對文本進行預處理
4.分解提取單詞 list
5.單詞計數字典 set , dict
6.按詞頻排序 list.sort(key=lambda),turple
7.排除語法型詞彙,代詞、冠詞、連詞等無語義詞
8.輸出TOP(20)
import pandas as pd def getText(): txt = open(r"E:\KING\PyCharm\KINGKING\big.txt", "rt").read() txt = txt.lower() for ch in '''’!@#$%^&*()_+=-';":.,<>/?|''': txt.replace(ch, " ") wordlist = txt.split() return wordlist # 詞頻統計 wordlist = getText() # 過濾(排除語法詞彙,帶刺,冠詞,連詞等) mum = {'it', 'if', 'the', 'at', 'for', 'on', 'and', 'in', 'to', 'of', 'a', 'was', 'be', 'were', 'in', 'about', 'from', 'with', 'without', 'an', 'one', 'another', 'others', 'that', 'they', 'himself', 'itself', 'themselves', 'if', 'when', 'before', 'though', 'although', 'while', 'as', 'as long as', 'i', 'he', 'him', 'she', 'out', 'is', 's', 'no', 'not', 'you', 'me', 'his', 'but'} wordset = set(wordlist) - mum # 字典 worddict = {} for w in wordset: worddict[w] = wordlist.count(w) # 詞頻排序 wordsort = list(worddict.items()) wordsort.sort(key=lambda x: x[1], reverse=True) for i in range(20): print(wordsort[i]) pd.DataFrame(data=wordsort).to_csv(r'E:\\KING\\大三(二)\\big.csv', encoding='utf-8')
top20:
在線生成詞雲:
排序好的單詞列表word保存成csv文件
import pandas as pd
pd.DataFrame(data=word).to_csv('big.csv',encoding='utf-8')
線上工具生成詞雲:
https://wordart.com/create