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

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

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

一、列表

#列表的增
lb=['aa','bb','哈','cc']
lb.append(456)

#列表的刪除
lb=['aa','bb','哈','cc']
del lb[3]

#列表的改
lb=['aa','bb','哈','cc']
lb[1]=123

#列表的查
lb=['aa','bb','哈','cc']
if 'bb' in lb:
    index = lb.index('bb')
print('列表的查:',index)

#列表的遍歷
lb=['aa','bb','哈','cc']
for i in lb:
    print(format(i),format(lb.index(i)+1))

 結果以下:安全

二、元組

#元組的對接
tup1=('aa','bb','cc','dd')
tup2=('ee','ff','gg','hh')
tup=tup1+tup2
print('元組的對接',tup)

#元組的刪除
元組不能刪除單個元素,只能刪除整個元組

#元組的改
元組不能修改元素值。

#元組的查
tup1=('aa','bb','cc','dd')
print(tup1[2])

#元組的遍歷
tup1=('aa','bb','cc','dd')
for i in tup1:
    print(i)

  結果圖:app

三、字典

#字典的增
dict={'aa':'11','bb':'22'}
dict['cc']='33'
print(dict)

#字典的刪
dict={'aa':'11','bb':'22'}
del dict['aa']
print(dict)

#字典的改
dict={'aa':'11','bb':'22'}
dict['aa']='33'
print(dict)

#字典的查
dict={'aa':'11','bb':'22'}
print(dict.get('aa'))

#字典的遍歷
dict={'aa':'11','bb':'22'}
for i in  dict:
    print(i+':'+dict[i])

  效果圖:函數

四、集合

#集合的添加
x={1,2,3}
x.add(4)
print(x)

#集合的刪除
x={1,2,3}
x.remove(1)
print(x)

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

一、列表

答:列表可讀寫,可重複,存儲方式是值,排列是有序列的python裏的列表用「[]」表示。工具

二、元組

答:元組和列表十分類似,不過元組是不可變的。即你不能修改元組。元組經過圓括號中用逗號分隔的項目定義。元組一般用在使語句或用戶定義的函數可以安全的採用一組值的時候,即被使用的元組的值不會改變。元組能夠嵌套。this

三、字典

答:字典相似於你經過聯繫人名稱查找地址和聯繫人詳細狀況的地址簿,即,咱們把鍵和值聯繫在一塊兒。鍵必須是惟一編碼

四、集合

答:與字典相似,但只包含鍵,而沒有對應的值,包含的數據不重複。orm

3、詞頻統計

要求:blog

  • 1.下載一長篇小說,存成utf-8編碼的文本文件 file排序

    2.經過文件讀取字符串 str

    3.對文本進行預處理

    4.分解提取單詞 list

    5.單詞計數字典 set , dict

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

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

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

  8.輸出TOP(20)

  • 9.可視化:詞雲

 排序好的單詞列表word保存成csv文件

import pandas as pd
pd.DataFrame(data=word).to_csv('big.csv',encoding='utf-8')

一、代碼

import operator

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

#刪除冠詞、帶詞、連詞等無心義詞語
for a in word2:
    if a not in dic:
        dic=dic
    else:
        del(dic[a])

swd = sorted(dic.items(), key=operator.itemgetter(1), reverse=True)
print(swd)

import pandas as pd
pd.DataFrame(data=swd).to_csv('big.csv',encoding='utf-8')
i=0
while True:
    print(swd[i])
    i=i+1
    if i == 19:
        break

  這裏我文章是經過打開TEXT文件來讀取的。


首先經過代碼把統計的結果保存爲csv,而後運用線上詞雲處理工具處理乘圖片。結果以下:

相關文章
相關標籤/搜索