开发环境

  1. Python版本: python3.6
  2. 调试工具:pycharm 2017.1.3
  3. 电脑系统:Windows 10 64位系统

统计列表中的数字频度

生成数据

from random import randint
data = [randint(-10, 10) for _ in range(30)]
print(data)

使用字典统计数据

使用迭代方法

from random import randint
data = [randint(-10, 10) for _ in range(30)]
print(data)
c = dict.fromkeys(data, 0)
print(c)
for x in data:c[x] += 1print(c)
[-7, -2, 8, 6, 3, -4, 10, -1, 2, -9, 5, 4, 8, -2, 3, 3, -10, -4, 2, 6, -9, -1, -3, 9, 6, -9, 3, -2, -7, -3]
{-7: 0, -2: 0, 8: 0, 6: 0, 3: 0, -4: 0, 10: 0, -1: 0, 2: 0, -9: 0, 5: 0, 4: 0, -10: 0, -3: 0, 9: 0}
{-7: 2, -2: 3, 8: 2, 6: 3, 3: 4, -4: 2, 10: 1, -1: 2, 2: 2, -9: 3, 5: 1, 4: 1, -10: 1, -3: 2, 9: 1}

使用标准库collections下的Counter函数

from random import randint
from collections import Counter
data = [randint(-10, 10) for _ in range(30)]
print(data)
c1 = Counter(data)
print(c1)
[4, 2, 4, -2, 7, 8, 2, -9, 3, 2, -8, 3, -9, -4, 10, 2, -1, 6, 9, 9, -10, 8, 8, -1, 10, -1, 0, -7, -10, 3]
Counter({2: 4, 8: 3, 3: 3, -1: 3, 4: 2, -9: 2, 10: 2, 9: 2, -10: 2, -2: 1, 7: 1, -8: 1, -4: 1, 6: 1, 0: 1, -7: 1})

找到出现频度最高的三个元素

from random import randint
from collections import Counter
data = [randint(-10, 10) for _ in range(30)]
print(data)
c1 = Counter(data)
print(c1)
res = c1.most_common(3)
print(res)
[9, -9, 4, -4, 0, -4, 7, 1, -7, 8, 10, -1, 1, 3, 6, 1, -9, -6, 7, -7, -3, 7, 7, 10, -8, -4, 6, 0, 6, -1]
Counter({7: 4, -4: 3, 1: 3, 6: 3, -9: 2, 0: 2, -7: 2, 10: 2, -1: 2, 9: 1, 4: 1, 8: 1, 3: 1, -6: 1, -3: 1, -8: 1})
[(7, 4), (-4, 3), (1, 3)]

统计英文文章词频统计

数据来源

import this

就会得到Python之禅

使用re.split()进行单词分割

import re
data = """The Zen of Python, by Tim PetersBeautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases aren't special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.Although that way may not be obvious at first unless you're Dutch.Now is better than never.Although never is often better than *right* now.If the implementation is hard to explain, it's a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- let's do more of those!"""
words = re.split('\W+', data)
print(words)
['', 'The', 'Zen', 'of', 'Python', 'by', 'Tim', 'Peters', 'Beautiful', 'is', 'better', 'than', 'ugly', 'Explicit', 'is', 'better', 'than', 'implicit', 'Simple', 'is', 'better', 'than', 'complex', 'Complex', 'is', 'better', 'than', 'complicated', 'Flat', 'is', 'better', 'than', 'nested', 'Sparse', 'is', 'better', 'than', 'dense', 'Readability', 'counts', 'Special', 'cases', 'aren', 't', 'special', 'enough', 'to', 'break', 'the', 'rules', 'Although', 'practicality', 'beats', 'purity', 'Errors', 'should', 'never', 'pass', 'silently', 'Unless', 'explicitly', 'silenced', 'In', 'the', 'face', 'of', 'ambiguity', 'refuse', 'the', 'temptation', 'to', 'guess', 'There', 'should', 'be', 'one', 'and', 'preferably', 'only', 'one', 'obvious', 'way', 'to', 'do', 'it', 'Although', 'that', 'way', 'may', 'not', 'be', 'obvious', 'at', 'first', 'unless', 'you', 're', 'Dutch', 'Now', 'is', 'better', 'than', 'never', 'Although', 'never', 'is', 'often', 'better', 'than', 'right', 'now', 'If', 'the', 'implementation', 'is', 'hard', 'to', 'explain', 'it', 's', 'a', 'bad', 'idea', 'If', 'the', 'implementation', 'is', 'easy', 'to', 'explain', 'it', 'may', 'be', 'a', 'good', 'idea', 'Namespaces', 'are', 'one', 'honking', 'great', 'idea', 'let', 's', 'do', 'more', 'of', 'those', '']

词频统计

import re
from collections import Counter
data = """The Zen of Python, by Tim PetersBeautiful is better than ugly.Explicit is better than implicit.Simple is better than complex.Complex is better than complicated.Flat is better than nested.Sparse is better than dense.Readability counts.Special cases aren't special enough to break the rules.Although practicality beats purity.Errors should never pass silently.Unless explicitly silenced.In the face of ambiguity, refuse the temptation to guess.There should be one-- and preferably only one --obvious way to do it.Although that way may not be obvious at first unless you're Dutch.Now is better than never.Although never is often better than *right* now.If the implementation is hard to explain, it's a bad idea.If the implementation is easy to explain, it may be a good idea.Namespaces are one honking great idea -- let's do more of those!"""
# 分割单词
words = re.split('\W+', data)
# 统计词频
c2 = Counter(words)
print(c2)
# 找出词频最高的10个单词
res = c2.most_common(10)
print(res)
Counter({'is': 10, 'better': 8, 'than': 8, 'to': 5, 'the': 5, 'of': 3, 'Although': 3, 'never': 3, 'be': 3, 'one': 3, 'it': 3, 'idea': 3, '': 2, 'should': 2, 'obvious': 2, 'way': 2, 'do': 2, 'may': 2, 'If': 2, 'implementation': 2, 'explain': 2, 's': 2, 'a': 2, 'The': 1, 'Zen': 1, 'Python': 1, 'by': 1, 'Tim': 1, 'Peters': 1, 'Beautiful': 1, 'ugly': 1, 'Explicit': 1, 'implicit': 1, 'Simple': 1, 'complex': 1, 'Complex': 1, 'complicated': 1, 'Flat': 1, 'nested': 1, 'Sparse': 1, 'dense': 1, 'Readability': 1, 'counts': 1, 'Special': 1, 'cases': 1, 'aren': 1, 't': 1, 'special': 1, 'enough': 1, 'break': 1, 'rules': 1, 'practicality': 1, 'beats': 1, 'purity': 1, 'Errors': 1, 'pass': 1, 'silently': 1, 'Unless': 1, 'explicitly': 1, 'silenced': 1, 'In': 1, 'face': 1, 'ambiguity': 1, 'refuse': 1, 'temptation': 1, 'guess': 1, 'There': 1, 'and': 1, 'preferably': 1, 'only': 1, 'that': 1, 'not': 1, 'at': 1, 'first': 1, 'unless': 1, 'you': 1, 're': 1, 'Dutch': 1, 'Now': 1, 'often': 1, 'right': 1, 'now': 1, 'hard': 1, 'bad': 1, 'easy': 1, 'good': 1, 'Namespaces': 1, 'are': 1, 'honking': 1, 'great': 1, 'let': 1, 'more': 1, 'those': 1})
[('is', 10), ('better', 8), ('than', 8), ('to', 5), ('the', 5), ('of', 3), ('Although', 3), ('never', 3), ('be', 3), ('one', 3)]

[Python高效编程] - 统计元素出现频度相关推荐

  1. 提高Python编程的效率技巧你知道哪些?收藏必备系列,阿里表哥推荐!Python高效编程技巧

    Python高效编程技巧 工作中经常要处理各种各样的数据,遇到项目赶进度的时候自己写函数容易浪费时间. Python 中有很多内置函数帮你提高工作效率! 一:在列表,字典中根据条件筛选数据 1.假设有 ...

  2. python多线程挂了_python多线程输入的问题 python高效编程技巧13(如何在线程之间实现...

    python3 创建线程时不用args传参,执行线程时为什如果创建线程时在target里就传入了参数,为什么在启动线程时,线程不是在Python多线程下,每个线程的执行方式: 有什么了不起,大不了继续 ...

  3. 你不知道的18个Python高效编程技巧

    来源 | Python编程时光 初识Python语言,觉得python满足了我上学时候对编程语言的所有要求.python语言的高效编程技巧让我们这些大学曾经苦逼学了四年c或者c++的人,兴奋的不行不行 ...

  4. 编程软件python中的if用法-Python高效编程的19个技巧

    初识Python语言,觉得python满足了我上学时候对编程语言的所有要求.python语言的高效编程技巧让我们这些大学曾经苦逼学了四年c或者c++的人,兴奋的不行不行的,终于解脱了.高级语言,如果做 ...

  5. python高效编程15个利器_你不知道的18个Python高效编程技巧

    来源 | Python编程时光 初识Python语言,觉得python满足了我上学时候对编程语言的所有要求.python语言的高效编程技巧让我们这些大学曾经苦逼学了四年c或者c++的人,兴奋的不行不行 ...

  6. 18 个 Python 高效编程技巧,Mark!

    点击"小詹学Python",选择"置顶"公众号 重磅干货,第一时间送达 本文转载自Python数据科学,禁二次转载 初识Python语言,觉得python满足了 ...

  7. 干货 | 18个 Python 高效编程技巧

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 初识Python语言,觉得python满足了我上学时候对编程语言的 ...

  8. Python高效编程的19个技巧

    初识Python语言,觉得python满足了我上学时候对编程语言的所有要求.python语言的高效编程技巧让我们这些大学曾经苦逼学了四年c或者c++的人,兴奋的不行不行的,终于解脱了.高级语言,如果做 ...

  9. 爱了,这18个 Python 高效编程技巧真香

    初识Python语言,觉得python满足了我上学时候对编程语言的所有要求.python语言的高效编程技巧让我们这些大学曾经苦逼学了四年c或者c++的人,兴奋的不行不行的,终于解脱了.高级语言,如果做 ...

  10. 18 个 Python 高效编程小技巧

    初识Python语言,觉得python满足了你上学时候对编程语言的所有要求.python语言的高效编程技巧让那些曾经苦逼学了四年c或者c++的人,兴奋的不行不行的,终于解脱了.高级语言,如果做不到这样 ...

最新文章

  1. 今日工作总结及计划: 2022-02-14
  2. AWS re-Invent最新发布AI产品解析:场景为王
  3. 数据中心供电有多重要,看看这件事就知道了
  4. C/C++快速读写磁盘数据的方法-块读取/异步/优化分析算法/内存文件映射的原理和使用
  5. Android用户界面设计“.NET研究”:框架布局
  6. 仿 小米运动_小米有品上架“黑科技”床垫,让你睡在“空气”上,改变睡眠体验...
  7. 矿难让显卡压了那么多货咋办?NV如是说
  8. C++工作笔记-对结构体中位域的补坑说明
  9. React Native(四)——顶部以及底部导航栏实现方式
  10. 第十一篇:Mysql系列
  11. 网络爬虫-2018个人总结
  12. 图片太模糊?这几个工具可以将图片变清晰
  13. 申克称重控制器维修称重仪表VEG20612/VDB20600
  14. c# 调用巴斯勒相机 进行图像识别
  15. SQL中模糊查询 like使用
  16. 大数据 排错日记0004——Unable to check if JNs are ready for formatting
  17. 怎么修改打印机服务器权限,打印机管理_怎样设置打印机管理权限
  18. 数字图像处理-车牌定位
  19. 一夜暴跌43%!电子烟行业“血流成河”,巨头也撑不住
  20. hbase 二进制数据写入_Hbase对于大对象存储的三种主要解决方案

热门文章

  1. Base64 编码的特征
  2. C++内部链接与外部链接
  3. walsh64码 matlab,实验7 Walsh码及单用户CDMA系统直接序列扩频仿真
  4. android音乐视频播放器,android音乐视频播放器.doc
  5. Linux 查看与修改mtu值
  6. bootstrap-select 的多选+模糊查询下拉框详解
  7. python做得好玩游戏免费
  8. 一步精准获取京东商品评论数据
  9. 关于视频编码I P B帧存储,编码,解码,显示顺序的理解
  10. 边境的悍匪—机器学习实战:第十三章 使用TensorFlow加载和预处理数据