复合数据类型

作业来源于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/2696

1.列表,元组,字典,集合分别如何增删改查及遍历。

2.总结列表,元组,字典,集合的联系与区别。参考以下几个方面:

  • 括号
  • 有序无序
  • 可变不可变
  • 重复不可重复
  • 存储与查找方式

3.词频统计

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

线上工具生成词云:
https://wordart.com/create

作业博客要求:

  • 文字作业要求言简意骇,用自己的话说明清楚。
  • 编码作业要求放上代码,加好注释,并附上运行结果截图。

答:

exclude={'a','i','you','and','the','to','be','is','in','or','will'}#定义停用词表
f=open('news.txt','r',encoding='utf-8')#打开文件
text=f.read()
print('text')
f.close()
text=text.lower()
sep=',.?;'
for s in sep:
    text=text.replace(s,' ')
bigList = text.split()#把text转化为List列表
print(bigList)
print('you',bigList.count('you'))
bigSet = set(bigList)#把List列表转换为集合
bigSet=bigSet-exclude#去掉停用词
print(bigSet)
bigDict={}#把集合转换为字典
for word in bigSet:
    bigDict[word]=bigList.count(word)
print(bigDict)
print(bigDict.items())
word = list(bigDict.items())
word.sort(key=lambda x:x[1],reverse=True)#排列
print(word)
import pandas as pd#生成词云
pd.DataFrame(data=word).to_csv('star.csv',encoding='utf-8')

猜你喜欢

转载自www.cnblogs.com/hjlaaa/p/10509639.html