天天看點

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

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

一.清單,元組,字典,集合分别如何增删改查及周遊

1、清單

#清單的增
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))
      

 結果如下:

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

2、元組

#元組的對接
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)
      

  結果圖:

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

3、字典

#字典的增
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])
      

  效果圖:

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

4、集合

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

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

二、總結清單,元組,字典,集合的聯系與差別。

答:清單可讀寫,可重複,存儲方式是值,排列是有序列的python裡的清單用“[]”表示。

答:元組和清單十分相似,不過元組是不可變的。即你不能修改元組。元組通過圓括号中用逗号分隔的項目定義。元組通常用在使語句或使用者定義的函數能夠安全的采用一組值的時候,即被使用的元組的值不會改變。元組可以嵌套。

答:字典類似于你通過聯系人名稱查找位址和聯系人詳細情況的位址簿,即,我們把鍵和值聯系在一起。鍵必須是唯一

答:與字典類似,但隻包含鍵,而沒有對應的值,包含的資料不重複。

三、詞頻統計

要求:

  • 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')      

1、代碼

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,然後運用線上詞雲處理工具處理乘圖檔。結果如下:      
複合資料類型,英文詞頻統計
下一篇: HDFS練習