複合數據類型,英文詞頻統計

做業來源於:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2696

1、列表,元組,字典,集合分別如何增刪改查及遍歷?

一、列表

list=['Jack','Lucy','Mary']
list.append('Pony')
print("末尾增長元素 :{}".format(list))

list.insert(1,"Lili")
print("指定位置增長元素 :{}".format(list))

list=['Jack','Lucy','Mary']
list.remove('Jack')
print("刪除元素 :{}".format(list))

list=['Jack','Lucy','Mary']
list.pop(0)
print("刪除元素 :{}".format(list))

list=['Jack','Lucy','Mary']
list[0]='a'
print("修改元素 :{}".format(list))

list=['Jack','Lucy','Mary']
print("查找元素 :{}".format(list[0]))

print("遍歷列表")
for l in list:
    print("{} :{}".format(list.index(l),l))

二、元組

tup=('Jack','Lucy',0)
tup2=('Lili',)
tup3=tup+tup2
print("鏈接/增長元素 :{}".format(tup3))

tup=('Jack','Lucy',0)
print("訪問元素 :tup[2]={},tup[0:1]={}".format(tup3[2],tup[0:2]))

tup=('Jack','Lucy',0)
print("刪除元祖")
del tup

tup=('Jack','Lucy',0)
print("遍歷元組:")
for t in tup:
    print(t)

三、字典

dict={'Jack':90,'Mary':80,'Tony':70}
dict["abc"]=100
print("增長abc:{}".format(dict))

dict={'Jack':90,'Mary':80,'Tony':70}
del dict['Jack']
print("刪除Jack:{}".format(dict))

dict={'Jack':90,'Mary':80,'Tony':70}
dict.pop('Tony')
print("刪除Tony:{}".format(dict))

dict={'Jack':90,'Mary':80,'Tony':70}
dict['Tony']=99
print("修改Tony的值:{}".format(dict))
dict['a']=dict.pop('Jack')
print("修改Tony的鍵:{}".format(dict))

dict={'Jack':90,'Mary':80,'Tony':70}
print("查找Mary的值:{}".format(dict.get('Mary')))

print("遍歷字典:")
for d in dict:
    print("{} : {}".format(d,dict[d]))

四、集合

a=set('abcd')
a.add('z')
print("增長'z':",a)
a.update({1,2})
print("增長 1,2:",a)

b=set(('a','b','c'))
b.remove('a')
print("刪除a :",b)

a=set('abcd')
a.discard('b')
print("刪除a :",a)

a=set('Jack')
a.pop()
print("pop刪除 :",a)

a=set('Jack')
a.clear()
print("清空集合 :",a)

a=set('Jack')
b=set(('1','2'))
c=set.union(a,b)
print("集合的合集",c)

print("遍歷集合:")
for s in c:
    print(s)

2、總結列表,元組,字典,集合的聯繫與區別。參考如下幾個方面:括號、有序無序、可變不可變、重複不可重複、存儲與查找方式。

列表:[],有序,可變,可重複,按值存儲,序列中的每一個元素都分配一個索引,按索引號查找,元素能夠是任意類型,可切片

元組:(),有序,與列表相似,但不可變,添加元素時用逗號隔開,按索引號查找,可切片

字典:{},有序,可變容器模型,可存儲任意類型對象,按key:value形式存儲,但key不可重複,不可切片

集合:(),無序,不可重複,建立格式:set()或parame = {value01,value02,...},每一個元素能夠是列表,元組,字典,不可切片

3、詞頻統計

要求:對文本進行預處理、去掉停用詞並排序輸出

源代碼:

fo = open(r'G:\test\TheLittlePrince.txt', encoding='utf-8-sig')
theLittlePrinceTxt = fo.read()
txt = theLittlePrinceTxt.lower()
fo.close()
sep = '''  ,./:?/! '\n  " [] ()  ~ '''
stops = {'by', 'his', 'their', 'again', 'off', 'where', 'now', 'up', 'this', 'before', 'which', 'after', 'a', 'then',
 "haven't", 'weren', 'll', 'down', 'or', 'no', "shan't", 'herself', 'in', 'some', 'such', "she's", 'does', 'nor', 
'just', "won't", 'them', 'further', 'how', 'am', 'mightn', 'it', 'too', 'ourselves', 'is', 'couldn', 'themselves', 
'should', 'ain', 'o', 'hadn', 'under', 'shan', 'him', "it's", 've', 'to', "don't", 'at', 'these', 'our', 'same', 'between', 
"you'd", 'isn', 'yourselves', 'until', 't', "mustn't", 'didn', 'few', 'each', 're', 'through', 'above', 'all', "you're", 
'been', 'hers', 'have', 'being', 'if', 'theirs', 'most', "doesn't", "hasn't", 'an', 'and', 'below', "couldn't", 'i', 'we', 
"hadn't", 'mustn', 'about', "shouldn't", 'there', 'her', 'y', 'here', 'was', "isn't", "needn't", 'were', 'haven', 'out', 
'ours', 'over', 'once', 'having', 'against', 'don', 'has', 'but', 'wouldn', 'with', 'other', 'doesn', 'itself', 'aren', 'when',
 'as', "wasn't", 'myself', "you'll", 'because', 'the', 'so', "didn't", 'are', 'for', "you've", 'd', 'hasn', 'wasn', 'on', 'he', 
's', 'of', 'they', 'needn', 'ma', 'while', 'than', 'from', "weren't", 'those', 'what', 'who', 'himself', "should've", 'will', 
'whom', 'more', "mightn't", 'do', 'its', 'why', 'only', 'that', 'during', 'not', 'had', 'own', 'm', 'me', 'very', 'doing', 'can',
 'be', 'my', 'both', 'into', "aren't", 'shouldn', 'won', 'yours', 'did', 'you', 'she', 'yourself', 'your', "wouldn't", 'any', "that'll"} 
 # 定義停用詞

for s in sep:
    txt = txt.replace(s, " ")

allWord = txt.split()
mset = set(allWord)  # 去掉重複的單詞,將文本轉換爲集合
mset = mset - stops  # 去除停用詞
mdict = {}  # 定義字典,用於輸出

for m in mset:
    mdict[m] = allWord.count(m)  # 統計每一個字典的key的頻數

mlist = list(mdict.items())  # 字典轉換成列表
mlist.sort(key=lambda x: x[1], reverse=True)  # 列表排序
print(mlist[0:10])
print(mlist[10:20])  #輸出top20

import pandas as pd  # 在默認目錄生成csv文件
pd.DataFrame(data=mlist).to_csv('Little.csv', encoding='utf-8')

結果:


在線詞雲統計

相關文章
相關標籤/搜索