1.利用字典和列表表达式统计英文单词个数

sentence="""Travel policies for this year's Spring Festival, which will fall in early February, should be devised based on COVID-19 risk appraisals of different regions and groups, health officials and experts said on Saturday."Whether or not it is necessary to stay put for the Spring Festival should be based on risk evaluations and should not be uniform across the country," said Liang Wannian, a national-level disease control expert, during a news briefing.He said increased movement during the Spring Festival travel rush, coupled with diminished immunity against respiratory diseases during winter, will indeed lead to heightened risk of the spread of COVID-19 and other infectious illnesses.However, China has also gained several advantages, such as its high COVID-19 vaccination coverage, and its prompt control of local outbreaks."Most recent outbreaks are linked to imported cases," he said. "As long as we can strictly implement policies to control imported cases, and the public can practice personal protective measures, we are capable of stemming the virus' spread."He said key regions and populations should abide by strict virus control measures."High risk groups, such as the elderly, people with chronic diseases and pregnant women should cut unnecessary trips and avoid gatherings, while the remaining population should adhere to protection measures," he said."""

dic={char:sentence.count(char) for char in set(sentence.split()) if char.isalpha()}  #生成字典,并且去除了标点符号。如果是根据字母来排序的话把sentence.split()改为sentence即可。
print(sorted(dic.items(),key=lambda x:x[1],reverse=True))  #根据键值进行排序。

显示结果:
[('a', 94), ('in', 22), ('on', 18), ('he', 18), ('as', 15), ('is', 12), ('the', 11), ('and', 9), ('or', 7), ('of', 6), ('should', 6), ('said', 6), ('to', 6), ('it', 6), ('control', 4), ('risk', 4), ('Festival', 3), ('be', 3), ('disease', 3), ('during', 3), ('we', 3), ('for', 3), ('Spring', 3), ('strict', 2), ('He', 2), ('diseases', 2), ('imported', 2), ('based', 2), ('spread', 2), ('regions', 2), ('policies', 2), ('its', 2), ('outbreaks', 2), ('necessary', 2), ('can', 2), ('such', 2), ('virus', 2), ('with', 2), ('not', 2), ('will', 2), ('population', 2), ('are', 2), ('practice', 1), ('strictly', 1), ('travel', 1), ('movement', 1), ('uniform', 1), ('protection', 1), ('public', 1), ('chronic', 1), ('immunity', 1), ('against', 1), ('trips', 1), ('cut', 1), ('Travel', 1), ('adhere', 1), ('health', 1), ('recent', 1), ('respiratory', 1), ('also', 1), ('while', 1), ('this', 1), ('linked', 1), ('devised', 1), ('personal', 1), ('other', 1), ('populations', 1), ('capable', 1), ('diminished', 1), ('increased', 1), ('stay', 1), ('long', 1), ('different', 1), ('Liang', 1), ('China', 1), ('several', 1), ('heightened', 1), ('put', 1), ('news', 1), ('indeed', 1), ('vaccination', 1), ('unnecessary', 1), ('appraisals', 1), ('gained', 1), ('prompt', 1), ('people', 1), ('protective', 1), ('pregnant', 1), ('by', 1), ('officials', 1), ('stemming', 1), ('women', 1), ('evaluations', 1), ('infectious', 1), ('avoid', 1), ('early', 1), ('key', 1), ('experts', 1), ('local', 1), ('implement', 1), ('across', 1), ('coupled', 1), ('lead', 1), ('fall', 1), ('which', 1), ('remaining', 1), ('high', 1), ('has', 1), ('abide', 1)]

2.  利用字典和列表表达式统计汉字个数

sentence="""3.算法的基本要素:一是对数据对象的运算和操作;二是算法的控制结构。
4.指令系统:一个计算机系统能执行的所有指令的集合。
5.基本运算包括:算术运算、逻辑运算、关系运算、数据传输。
6.算法的控制结构:顺序结构、选择结构、循环结构。
7.算法基本设计方法:列举法、归纳法、递推、递归、减斗递推技术、回溯法。
8.算法复杂度:算法时间复杂度和算法空间复杂度。 两个之间没有联系的。
9.算法时间复杂度是指执行算法所需要的计算工作量。
10.算法空间复杂度是指执行这个算法所需要的内存空间。"""
d={char:sentence.count(char) for char in set(sentence) if "u4e00"<=char<="\u9fff"}
print(sorted(d.items(),key=lambda x:x[1],reverse=True))

运算结果:

[('算', 19), ('法', 15), ('、', 10), ('的', 9), ('。', 9), ('间', 6), ('运', 5), ('构', 5), ('度', 5), ('结', 5), ('杂', 5), ('复', 5), ('系', 4), ('是', 4), ('指', 4), ('要', 3), ('执', 3), ('计', 3), ('本', 3), ('基', 3), ('所', 3), ('个', 3), ('空', 3), ('行', 3), ('递', 3), ('和', 2), ('归', 2), ('统', 2), ('制', 2), ('控', 2), ('作', 2), ('推', 2), ('需', 2), ('有', 2), ('令', 2), ('术', 2), ('对', 2), ('一', 2), ('据', 2), ('数', 2), ('时', 2), ('回', 1), ('存', 1), ('传', 1), ('环', 1), ('斗', 1), ('方', 1), ('素', 1), ('量', 1), ('集', 1), ('内', 1), ('之', 1), ('这', 1), ('辑', 1), ('没', 1), ('象', 1), ('操', 1), ('技', 1), ('减', 1), ('二', 1), ('举', 1), ('设', 1), ('包', 1), ('两', 1), ('输', 1), ('顺', 1), ('联', 1), ('序', 1), ('工', 1), ('择', 1), ('列', 1), ('括', 1), ('选', 1), ('循', 1), ('合', 1), ('逻', 1), ('能', 1), ('机', 1), ('关', 1), ('纳', 1), ('溯', 1)]

3. 运用字典的Get命令和循环来统计汉字个数。

sentence="""人民 社会 国家 科学 人民 社会 好 好 好 好"""
lst=sentence.split()
dic={}
for i in lst:
    dic[i]=dic.get(i,0)+1 #用dict的get方法,找到键名为i的就返回其对应的值,否则就返回0
print(sorted(dic.items(),key=lambda x:x[1],reverse=True))

显示结果:

[('好', 4), ('人民', 2), ('社会', 2), ('国家', 1), ('科学', 1)]

4.  用NLTK中的FreqDist函数

import nltk
from nltk import FreqDist
s="life is short and I like python."
freq = FreqDist(list(s))
for key in freq:
    if key.isalpha():
        print(key,freq[key])

结果展示:

i 3
l 2
e 2
s 2
h 2
o 2
t 2
n 2
f 1
r 1
a 1
d 1
I 1
k 1
p 1
y 1

5. 通过交换键值对的位置进行排序

把字典转化为元组以后再排序:

lst=sentence.split()
dic={}
for i in lst:
    dic[i]=dic.get(i,0)+1 #用dict的get方法,找到键名为i的就返回其对应的值,否则就返回0

ls=sorted([(p[1],p[0]) for p in dic.items()],reverse=True)

print([(p[1],p[0]) for p in ls])

[('好', 4), ('人民', 2), ('社会', 2), ('国家', 1), ('科学', 1)]

Python统计字符/单词/汉字个数(字典)的三种方法相关推荐

  1. 统计内表行数常用的三种方法

    以下是统计内表行数常用的三种方法, 如下:   1.LOOP AT it_itab.    g_lines2 = g_lines2 + 1.    ENDLOOP.    该方法是通过循环内部表自己累 ...

  2. Python课堂:判定IP地址合法性的三种方法

    IP IP合法性校验是开发中非常常用的,看起来很简单的判断,作用确很大,写起来比较容易出错,今天我们来总结一下,看一下3种常用的IP地址合法性校验的方法. IPv4的ip地址格式:(1~255).(0 ...

  3. C语言函数怎么像python那样返回多个值?(三种方法:1、设置全局变量 2、传递指针 3、使用结构体返回不同类型的数据)

    引用文章:c语言函数可不可以返回多个值 文章目录 方法一:设置全局变量 例如:利用一个函数求出正方形的周长和面积. 方法二:使用数组名或指针作为函数的形参 实例2:编写函数求一维整形数组的最大值与最小 ...

  4. python如何删除代码_Python列表删除的三种方法代码分享

    1.使用del语句删除元素 >>> i1 = ["a",'b','c','d'] >>> del i1[0] >>> prin ...

  5. 计算两个数平均值的三种方法

    第一种方法: 思路:两数相加求和再除2 代码 int a=10;int b=20;int avg=0;avg=(a+b)/2;//平局值//或者avg=(a+b)>>1://右移表示除2, ...

  6. 计算两个数平均数的三种方法

    第一种方法 思路:两数相加然后除以2 参考代码: #include<stdio.h>int main(){ int a = 10;int b = 20;int aver = (a + b) ...

  7. python 解方程_python如何解方程的三种方法

    python求解方程组的三种方法: Numpy求解方程组x + 2y = 3 4x + 5y = 6 当然我们可以手动写出解析解,然后写一个函数来求解,这实际上只是用 Python 来单纯做" ...

  8. python解复杂方程_Python 解方程的三种方法

    # 首发于我的博客 The North. 新年第一篇,搞起. 这回写一个好久之前想做,一直搁着没做的东西-- Python 解方程(其实是放假回家,趁着家里电脑重装 LOL 的时间过来写一篇). 咱这 ...

  9. Python 炫技操作:合并字典的七种方法

    来源 | Python编程时光(ID: Cool-Python) Python 语言里有许多(而且是越来越多)的高级特性,是 Python 发烧友们非常喜欢的.在这些人的眼里,能够写出那些一般开发者看 ...

最新文章

  1. FPGA的设计艺术(3)静态时序分析
  2. 基于Vue.js的精选壁纸推荐单页应用
  3. android 回退函数,详解React Native监听Android回退按键与程序化退出应用
  4. 数据库表的字段中含空格怎么办?
  5. 【加解密学习笔记:第三天】OllyDbg断点介绍
  6. c# out关键字 vb_c# 关键字:ref 和 out
  7. php css去除h1样式,HTML中怎么设置h1的字体样式你知道吗?关于设置h1标签的样式详解...
  8. golang 内存分析/动态追踪
  9. echarts多次使用SetOption时的数据问题
  10. 【风电功率预测】基于matlab EMD优化LSTM风电功率预测【含Matlab源码 1402期】
  11. camtasia喀秋莎2022(屏幕录像课件制作工具)
  12. 关于Hilbert矩阵的几道编程题
  13. SAP Hybris培训-标准课程/企业内训
  14. 45-js操作DOM和bom操作
  15. Linux嵌入式数据库
  16. 浙江大学计算机陈曦博士,浙江大学控制科学与工程学院博士生导师简介:陈曦...
  17. 超边际分析不能用计算机,超边际分析方法
  18. 如何查看小方侦测云存储_小方智能摄像机和手机怎样连接?
  19. 音乐播放,进度条,上一首下一首,暂停/播放
  20. 计算机与信息安全+虚拟仿真Vr,VR虚拟仿真技术给高校实验教学安全多一道防线...

热门文章

  1. 实验报告 LINUX组管理,Linux实验报告-用命令实现用户和组的管理.doc
  2. 用Python DIY二维码背景
  3. PDM系统的产生及相关技术理论
  4. java opencv 阀值分割_opencv 阈值分割的具体使用
  5. 基于javaweb的网上商城系统(java+jsp+servlert+mysql+ajax)
  6. WIFI破解系列:(03)VMware Workstation虚拟机如何连接usb网卡
  7. 攻防世界MISC_base64stego
  8. POJ - 2031 Building a Space Station (最小生成树)
  9. 研究综述 - TKDE2020 | 基于知识图谱的推荐系统
  10. 非数值表示字符和汉字表示、声音表示、图像表示