Python3简单的基础知识(三)

该博客进行了Python知识的回顾,涵盖函数、组合数据类型、递归思想、pyinstaller库等内容。还着重提及字典数据类型、jieba库与词频统计、文件操作、数组与格式化,以及wordcloud库等信息技术相关知识。

前篇回顾

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值