目录

  • 词频统计及个性化输出
    • 1. 所需库的安装
    • 2. jieba库小Demo
    • 3. CalHamlet 字符统计
    • 4. CalHamlet 字符统计 过滤
    • 5.三国演义 人物出场统计
    • 6.三国演义 人物出场统计 过滤版
    • 7.杨辉三角
    • 8.简单的词云小程序

词频统计及个性化输出

1. 所需库的安装

由于正常操作安装太慢,所以使用豆瓣的镜像库进行安装

pip3 install jieba -i https://pypi.douban.com/simple

pip3 install wordcloud -i https://pypi.douban.com/simple

pip3 install imageio -i https://pypi.douban.com/simple

2. jieba库小Demo

import jieba
jieba.lcut("中国是一个伟大的国家")
['中国', '是', '一个', '伟大', '的', '国家']

3. CalHamlet 字符统计

def getText():txt = open("hamlet.txt","r").read()txt = txt.lower()for ch in '!"#$%^&*()+_-=,./:;<>?[\\]{|}`~':txt = txt.replace(ch," ")return txthamletTxt = 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))
the        1138
and         965
to          754
of          669
you         550
i           542
a           542
my          514
hamlet      462
in          436

4. CalHamlet 字符统计 过滤

excludes = {"the","and","of","you","a","i","my","in","to","that","is","it","not","his","this","but","with","for","your","me","be","as","he","what","him","so","have","will","do","no","we","are","all","on","our","by","or","shall","if","o","good","come","thou","they","now","more","let","from","her","how","at","thy"}
def getText():txt = open("./资源/hamlet.txt","r").read()txt = txt.lower()for ch in '!"#$%^&*()+_-=,./:;<>?[\\]{|}`~':txt = txt.replace(ch," ")return txthamletTxt = getText()
words = hamletTxt.split()
counts = {}
for word in words:counts[word] = counts.get(word,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))
hamlet      462
lord        309
king        194
horatio     157
claudius    120
queen       117
polonius    116
laertes     103
gertrude     95
ophelia      86

5.三国演义 人物出场统计

import jieba
txt = open("./资源/三国演义.txt",'r',encoding='utf-8').read()
words = jieba.lcut(txt)
counts = {}
for word in words:if len(word)==1:continueelse:counts[word] = counts.get(word,0)+1
items = list(counts.items())
items.sort(key = lambda y:y[1],reverse=True)
for i in range(15):word , count  = items[i]print ("{0:<10}{1:>5}".format(word,count))
曹操          934
孔明          831
将军          759
却说          647
玄德          570
关公          509
丞相          488
二人          463
不可          435
荆州          420
孔明曰         384
玄德曰         383
不能          383
如此          376
张飞          348

6.三国演义 人物出场统计 过滤版

import jieba
excludes = {"将军","却说","荆州","二人","不可","不能","如此","商议"}
txt = open("./资源/三国演义.txt","r",encoding ='utf-8').read()
words = jieba.lcut(txt)
counts ={}
for word in words:if len(word)==1:continueelif word =="诸葛亮"or word=="孔明曰":rword == "孔明"elif word =="关公"or word=="云长":rword = "关羽"elif word =="玄德"or word=="玄德曰":rword = "刘备"elif word =="孟德"or word=="丞相":rword = "曹操"else:rword = wordcounts[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(5):word,count = items[i]print("{0:<10}{1:>5}".format(word,count))
曹操         1435
刘备         1228
孔明          839
关羽          779
张飞          348

7.杨辉三角

def NumList_to_StrList(data):new_data = []for i in range(len(data)):new_data.append(str(data[i]))string = ' '.join(new_data)return stringdef YangHui(n):width = n * 6print('1'.center(width))line = [1,1]print('1 1'.center(width))for i in range(2,n):r = []for j in range(0,len(line) - 1):r.append(line[j] + line[j + 1])line = [1] + r + [1]print(NumList_to_StrList(line).center(width))YangHui(6)
                 1                  1 1                 1 2 1                1 3 3 1               1 4 6 4 1              1 5 10 10 5 1

8.简单的词云小程序

import jieba
import wordcloud
import imageio
f = open("./资源/三国演义.txt","r",encoding ='utf-8').read()
ls = jieba.lcut(f)
txt = " ".join(ls)
m = imageio.imread("./资源/duye.jpg")
font = r'c:/Windows/Fonts/simfang.ttf'
w = wordcloud.WordCloud(background_color="white",font_path =font ,width=1000,height=1000,mask=m).generate(txt)
w.to_file("./资源/test.png")

Python词频统计与杨辉三角相关推荐

  1. 用python中函数输出杨辉三角_用Python输出一个杨辉三角的例子

    这篇文章主要介绍了用Python和erlang输出一个杨辉三角的例子,同时还提供了一个erlang版杨辉三角,需要的朋友可以参考下 关于杨辉三角是什么东西,右转维基百科:杨辉三角 稍微看一下直观一点的 ...

  2. Python中由生成杨辉三角代码所思考的一些问题

    杨辉三角定义如下: 1/ \1 1/ \ / \1 2 1/ \ / \ / \1 3 3 1/ \ / \ / \ / \1 4 6 4 1/ \ / \ / \ / \ / \ 1 5 10 10 ...

  3. Python程序:输出杨辉三角的几种办法

    文章目录 一.问题描述 二.问题分析 三.第一种方法 1.具体代码 2.运行结果 3.程序的改进 四.第二种方法 1.具体代码 2.运行结果 五.总结分析 一.问题描述 给定一个非负整数 n,生成「杨 ...

  4. python杨辉三角函数公式大全,Python使用函数实现杨辉三角

    运行效果: 可在函数中指定阶层数,输出对应的杨辉三角 源代码如下: 1 # -*-coding:utf-8 -*- 2 ''' 3 chapter4_do.py 4 函数yanghui(n)用于输出n ...

  5. python用generator打印杨辉三角_python写generator输出杨辉三角遇到问题,望高手解答!...

    >>>defy_():...L=[1]...whileTrue:...yieldL...L=L.append(0)...L=L.insert(0,0)...L=[L[x]+L[x+1 ...

  6. 曾经vb,c ,c++,python,写过的杨辉三角,致敬杨老前辈

    // 杨辉三角形,又称贾宪三角形,帕斯卡三角形,是二项式系数在三角形中的一种几何排列.杨辉三角形同时对应于二项式定理的系数. 直角三角形杨辉三角 //c语言,求直角的 #include<stdi ...

  7. python杨辉三角函数_python杨辉三角输出指定行_使用python打印十行杨辉三角过程详解...

    如何用python输出杨辉三角 程序输出需要实现如下效果: [1] [1,1] [1,2,1] [1,3,3,1] .. 方法:迭代,生成器 12345678910111213141516171819 ...

  8. python杨辉三角输出指定行_使用python打印十行杨辉三角过程详解

    如何用python输出杨辉三角 程序输出需要实现如下效果: [1] [1,1] [1,2,1] [1,3,3,1] .. 方法:迭代,生成器 12345678910111213141516171819 ...

  9. python杨辉三角输出指定行_python杨辉三角输出指定行_使用python打印十行杨辉三角过程详解...

    如何用python输出杨辉三角 程序输出需要实现如下效果: [1] [1,1] [1,2,1] [1,3,3,1] .. 方法:迭代,生成器 12345678910111213141516171819 ...

  10. 第四周python笔记 Python封装结构 哈希查找 杨辉三角 冒泡排序

    知识点:浅拷贝 字符串  封装解构 集合 ipython 哈希查找与线性查找  代码实现:杨辉三角 冒泡排序 list复制 是浅拷贝 简单类型 新开地址 拷贝数值 引用类型 只拷贝引用 直接拷贝地址 ...

最新文章

  1. C++ 文本查询2.0(逻辑查询)
  2. 项目管理中的十一个原则
  3. cesium 加载bim模型_构建统一CIM数字底盘,实现基于BIM的全流程管控
  4. Soul网关发布2.2.0:让高性能网关变得如此简单!
  5. 如何运用领域驱动设计 - 聚合
  6. 用R在地图上绘制网络图的三种方法
  7. android模块编译错误,android studio编译出错:Android resource linking failed
  8. 关于伪类:after和content的实际应用
  9. 手机版页面正式发布 html5取代wap(wml)
  10. 手机端html本地存储,HTML5 web storage本地存储
  11. ZigBee-CC2530单片机 - 按键控制跑马灯的启停
  12. 威富通实现微信支付概述
  13. 五种知网文献免费下载方式
  14. 在树莓派中解决浏览器自动更新失败弹框遮挡数据的方案
  15. 新款 Mac mini(2018) 性能及接口分析
  16. 请简述独占设备的分配过程。
  17. Zabbix 地址Ping检测告警
  18. MinGW与Clion下载安装及使用详解
  19. wi ndows怎么自动编页,15个小技巧,让我的Windows电脑更好用了!
  20. C#根据百家姓,将姓转拼音处理

热门文章

  1. Everything软件配置
  2. 方波信号的上升沿与带宽
  3. WebView下载点击无反应问题
  4. 单片机实验五、外部中断实验
  5. 哔哩哔哩手机客户端下载视频转到电脑上播放
  6. Python学习笔记—— python基础 1. 变量的输出
  7. c语言中英文字幕怎么相加,excel表格怎么快速分离中英文双语字幕? excel分离中英文字符串的技巧...
  8. 在哪下拼多多上传助手?拼多多软件方法介绍
  9. C语言二维数组定义、赋值、按要求遍历操作、输出以及函数调用
  10. m 文件 dll matlab 中调用_《淫巧系列二》MATLAB中直接调用Python