Python3简单的基础知识(三)
2019-03-22 07:52
239 查看
前篇回顾
1、函数
2、组合数据类型
3、递归思想
4、pyinstaller库
有关字典数据类型
#字典数据类型 ''' {}/dict()生成一个新的字典类型,正因为这点,集合数据类型不能用{}生成一个新的集合 {K:V} d[k]得到值 d[k]=newV更改 ''' #字典类型操作方式 ''' del d[k] k in d d.keys() d.values() d.items() d.get(k[,default]) d.pop(k[,default]) d.popitem() d.clear() len(d) ''' d={1:"p",2:"y"} d.popitem() print(d) #字典应用场景 #映射相关 #组合数据类型的核心:用一个变量恰当的表达一组数据的关系
有关jieba库与词频统计
#Exact mode split txt not exist extra vocabulary ''' list=jieba.lcut("中国是一个伟大的国家") ''' #Full mode split all possible vocabulary exist extra vocabulary ''' list=jieba.lcut("中国是一个伟大的国家",cut_all=True) ''' #Index mode base Exact mode split more ''' list=jieba.lcut_for_search("中国是一个伟大的国家") ''' #add jieba.add_word("蟒蛇语言") #完成对四级试题词频统计 def getText(path): return open("eng4.txt","r").read().lower() def getWords(text): return text.split() def statWords(path): excludes={"that","with","your"\ ,"they","have","will","what","their"\ ,"more","from","passage"\ ,"about","this"} text=getText(path) words=getWords(text) dic={} for word in words: if len(word) <=3: continue dic[word]=dic.get(word,0)+1 for c in excludes: del dic[c] items=list(dic.items()) items.sort(key = lambda x:x[1],reverse=True) for i in range(1000): word,num=items[i] print("{0:<5}{1:<15}{2:>8}".format(i,word,num)) def main(): path="eng4.txt" statWords(path) main()
有关文件
#文件 ''' 所有文件分为文本文件和二进制文件 文本文件有统一的编码格式rt 二进制文件没有统一的字符编码,由0和1组成rb 文件的处理步骤 打开--操作--关闭 打开时文件由存储状态变为占用状态 r w x a b t + 读 read([size=-1]) readline([size=-1]) 读取这一行的字符个数 readlines([hint=-1]) 写 write(str) write(lines) //不会分行 seek(光标位置) 0 1 2 开头 当前 结尾 注意:先写在读 光标位置 处理 read() read(1024) 搭配while for line in readlines for line in file ''' ''' 自动化 原理:从文件中解析数据,处理数据,是电脑自行完成 自动化思维:数据和功能分离,数据驱动的自动运行 接口化设计:格式化设计结构,清晰明了 二位数据应用:应用维度组织数据,二位数据最常用 ''' #自动化画图 import turtle def autoDraw(path): f=open(path,"r") for line in f: ls=line.split(",") go,direct,angle=map(eval,ls) turtle.fd(go) if direct==1: turtle.right(angle) else : turtle.left(angle) f.close() def main(): path="route.txt" autoDraw(path) main()
有关数组与格式化
#数据格式化 ''' 将一组数据按照一定规格和样式进行规范:表示、存储、运算等 ''' #一维数组:对应列表 元组/数组 集合 #二维数组:由多个一维数组构成,是一维数组的组合形式 #多维数据:由一维或二维在新维度上扩展形成 比如增加时间维度 #高维数据:仅使用最基本的二元关系展示数据间的复杂结构 #***数据的操作周期 ''' 数据存储-----------数据表示----------数据操作 存储格式-----------数据类型----------操作方式 ''' #一维数组 #分隔符 ''' 1、空格分割 2、逗号分隔 3、其他特殊字符分割 ''' #数据处理 ''' 读入数据 open(path,"w+").read().split() 写出数据 ls=["1","2","3"] open(path,"w").write(" ".join(ls)) ***** "分隔符".join(ls) 用分隔符分割数组 ''' #二维数据 #二位数据的表示 ''' 暂时用二维列表 ls=[[],[]] ''' #CSV Comma-Separated Values ''' 一般扩展名为.csv 用逗号存储数据的格式 数据转换之间通用的标准格式 ls[row][column] 先行后列 ''' #CSV读入 ''' ls=[] f=open(path,"r") for line in f: ls.append(line.replace("\n","").split(",")) f.close() ''' #CSV写出 ''' ls=[[],[],[]] f=open(path,"w") for item in ls: f.write(",".join(item)+"\n") f.close() '''
有关wordcloud库
#wordcloud lib #wordcloud.WordCloud([width=800][,height=600][,min_font_size=4]\ [,max_font_size=20][,font_step=1][,font_path=msyh.ttc]\ [,max_words=400][,stop_words={"python"}][,mask=mk][,background_color="black"]) #w.generate(txt) loading txt into object WordCloud #w.to_file(filename) print image file include .png .jpg step1: create object step2: loading txt step3: print image process1: split process2: statistics process3: font-size process4: font-style #specify shape ''' from scipy.misc import imread mk=imread("pic.png") w=wordcloud.WordCloud(mask=mk) ''' #政府报告词云 #Govement Statistic import jieba import wordcloud import imageio def getTxt(path): f=open(path,"rt",encoding="utf-8") file=f.read() f.close() ls=jieba.lcut(file) txt=" ".join(ls) return txt def main(): path="govReport.txt" txt=getTxt(path) mask=imageio.imread("timg.jpg") wc=wordcloud.WordCloud(font_path="msyh.ttc",width=1600,height=1200,\ background_color="white",max_words=20,mask=mask) wc.generate(txt) wc.to_file("gWordCloud.png") main()
相关文章推荐
- Python编程:从入门到实践 学习笔记 基础知识(一) 变量与简单数据类型
- python基础知识之字典的建立、简单操作、格式化字符串
- Python爬虫----基础知识(简单爬虫架构、URL管理器和实现方法)
- Python3简单的基础知识(四)
- 基础知识(十五)python简单http服务器
- Python 爬虫(1)基础知识和简单爬虫
- python-for和while基础知识、嵌套和简单的字符串知识
- Python爬虫----基础知识(简单爬虫架构、URL管理器和实现方法)
- python 基础知识回顾篇之简单迭代和列表生成
- Python简单入门语法python基础入门知识
- python爬虫入门(1) 基础知识 ; 正则表达式 Re 模块
- Python 第一章 基础知识
- python 基础知识快速入门
- python的一些基础知识
- python基础知识(引用)
- 【深度学习基础-11】简单线性回归(下)--实例及python代码实现
- Python中关于字符串对象的一些基础知识
- 基础知识(十三)dlib python人脸检测
- 委托基础知识简单介绍
- Python 学习总结(一):掌握基础知识,查缺补漏