1,求解基本统计值
#calstistics.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),0.5) #此处视频中为len(numbers)-1
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 #之前忘记了返回值,导致输出为None
n = getNum()
m = mean(n)
print("平均值:{},方差:{},中位数:{}.".format(m,dev(n,m),median(n)))
2, jieba库的安装与使用指令
在cmd命令行中输入
pip install jieba
若报错存在延时,也可输入
pip --default-timeout=100 install -U jieba
若仍然报错,则需要手动安装
(1)下载地址https://pypi.org/project/jieba/#files
(2)cmd进入jieba的文件python setup.py install
# #dpcq.txt
import jieba
excludes = {"便是","之中","能够","旋即","之上","却是","有些","有着","这种","然后"}
txt = open("dpcq.txt","r",encoding="ANSI").read()
words = jieba.lcut(txt) #切分文章
counts = {} #创建字典类型
for word in words:
if len(word) == 1:
continue
#可以添加elif
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(30):
word, count = items[i]
print("{0:<10}{1:>15}".format(word,count))