发布于2020-02-27 11:49 阅读(1105) 评论(0) 点赞(11) 收藏(4)
方法论- Python三种主流组合数据类型的使用方法
实践能力-学会编写处理一组数据的程序
练习(可选)- 5道编程@Python123
测验- 10道单选+2道编程@Python123
目录:
小结:
集合是多个元素的无序组合
集合间操作
集合操作符
6个操作符
4个增强操作符
集合类型的定义:
包含关系比较:
数据去重:集合类型所有元素无重复
目录:
-序列类型定义
-序列处理函数及方法
-元组类型及操作
-列表类型及操作
-序列类型应用场景
小结:
序列是具有先后关系的一组元素
序列是一个基类类型
序号的定义:
序列类型通用操作符
6个操作符
序列类型操作实例
序列类型通用函数和方法
5个函数和方法:
序列类型操作实例
元组类型定义元组是序列类型的一种扩展
元组类型操作:
元组继承序列类型的全部通用操作
列表类型定义:
列表是序列类型的一种扩展,十分常用
列表类型操作函数和方法
列表类型操作:
列表类型操作函数和方法:
列表类型操作:
列表功能默写:
数据表示:元组和列表
元素遍历
数据保护
基本统计值
基本统计值
基本统计值计算
#CalStatisticsV1.py
def getNum(): #获取用户不定长度的输入
nums = []
iNumStr = input("请输入数字(回车退出): ")
while iNumStr != "":
nums.append(eval(iNumStr))
iNumStr = input("请输入数字(回车退出): ")
return nums
def mean(numbers): #计算平均值
s = 0.0
for num in numbers:
s = s + num
return s / len(numbers)
def dev(numbers, mean): #计算方差
sdev = 0.0
for num in numbers:
sdev = sdev + (num - mean)**2
return pow(sdev / (len(numbers)-1), 0.5)
def median(numbers): #计算中位数
sorted(numbers)
size = len(numbers)
if size % 2 == 0:
med = (numbers[size//2-1] + numbers[size//2])/2
else:
med = numbers[size//2]
return med
n = getNum() #主体函数
m = mean(n)
print("平均值:{},方差:{:.2},中位数:{}.".format(m, dev(n,m),median(n)))
技术能力扩展
目录:
小结:
理解“映射”
字典类型是“映射”的体现
字典类型的用法
在字典变量中,通过键获得值
<字典变量> = {<键1>:<值1> , … , <键n>:<值n>}
<值> = <字典变量>[<键>]
<字典变量>[<键>] = <值>
[ ] 用来向字典变量中索引或增加元素
字典类型定义和使用
字典类型操作函数和方法
字典类型操作
字典类型操作函数和方法
字典类型操作
字典功能默写
映射的表达
-映射无处不在,键值对无处不在
-例如:统计数据出现的次数,数据是键,次数是值
-最主要作用:表达键值对数据,进而操作它们
元素遍历
for k in d :
<语句块>
jieba库概述
jieba是优秀的中文分词第三方库
-中文文本需要通过分词获得单个的词语
-jieba是优秀的中文分词第三方库,需要额外安装
-jieba库提供三种分词模式,最简单只需掌握一个函数
jieba库的安装
(cmd命令行) pip install jieba
jieba分词的原理
jieba分词依靠中文词库
jieba分词的三种模式
精确模式、全模式、搜索引擎模式
jieba库常用函数:
jieba分词要点:
jieba.lcut(s)
文本词频统计
需求 :一篇文章,出现了哪些词?哪些词出现得最多?
该怎么做呢?
英文文本➡️中文文本
英文文本:Hamet分析词频https://python123.io/resources/pye/hamlet.txt
中文文本:《三国演义》分析人物https://python123.io/resources/pye/threekingdoms.txt
#CalHamletV1.py
def getText():
txt = open("hamlet.txt", "r").read()
txt = txt.lower()
for ch in '!"#$%&()*+,-./:;<=>?@[\\]^_‘{|}~':
txt = txt.replace(ch, " ") #将文本中特殊字符替换为空格
return txt
hamletTxt = getText()
words = hamletTxt.split()
counts = {}
for word in words:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(10):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))
#CalThreeKingdomsV1.py
import jieba
txt = open("threekingdoms.txt", "r", encoding='utf-8').read()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1:
continue
else:
counts[word] = counts.get(word,0) + 1
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(15):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))
《三国演义》人物出场统计
将词频与人物相关联,面向问题
词频统计➡️人物统计
#CalThreeKingdomsV2.py
import jieba
excludes = {"将军","却说","荆州","二人","不可","不能","如此"}
txt = open("threekingdoms.txt", "r", encoding='utf-8').read()
words = jieba.lcut(txt)
counts = {}
for word in words:
if len(word) == 1:
continue
elif word == "诸葛亮" or word == "孔明曰":
rword = "孔明"
elif word == "关公" or word == "云长":
rword = "关羽"
elif word == "玄德" or word == "玄德曰":
rword = "刘备"
elif word == "孟德" or word == "丞相":
rword = "曹操"
else:
rword = word
counts[rword] = counts.get(rword,0) + 1
for word in excludes:
del counts[word]
items = list(counts.items())
items.sort(key=lambda x:x[1], reverse=True)
for i in range(10):
word, count = items[i]
print ("{0:<10}{1:>5}".format(word, count))
应用问题的扩展
作者:骏马
链接:https://www.pythonheidong.com/blog/article/235119/10ea0a3c225c6a1c10d7/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!