复合数据类型,英文词频统计

 改作业要求来源于: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)

二、总结列表,元组,字典,集合的联系与区别。

1、列表

答:列表可读写,可重复,存储方式是值,排列是有序列的python里的列表用“[]”表示。

2、元组

答:元组和列表十分相似,不过元组是不可变的。即你不能修改元组。元组通过圆括号中用逗号分隔的项目定义。元组通常用在使语句或用户定义的函数能够安全的采用一组值的时候,即被使用的元组的值不会改变。元组可以嵌套。

3、字典

答:字典类似于你通过联系人名称查找地址和联系人详细情况的地址簿,即,我们把键和值联系在一起。键必须是唯一

4、集合

答:与字典类似,但只包含键,而没有对应的值,包含的数据不重复。

三、词频统计

要求:

  • 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,然后运用线上词云处理工具处理乘图片。结果如下:

猜你喜欢

转载自www.cnblogs.com/cmhbaba/p/10513696.html
今日推荐