【python cookbook】【数据结构与算法】12.找出序列中出现次数最多的元素
2023-09-14 08:59:26 时间
问题:找出一个元素序列中出现次数最多的元素是什么
解决方案:collections模块中的Counter类正是为此类问题所设计的。它的一个非常方便的most_common()方法直接告诉你答案。
# Determine the most common words in a list words = [ 'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes', 'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the', 'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into', 'my', 'eyes', "you're", 'under' ] from collections import Counter word_counts = Counter(words) top_three = word_counts.most_common(3) print(top_three) # outputs [('eyes', 8), ('the', 5), ('look', 4)] # Example of merging in more words morewords = ['why','are','you','not','looking','in','my','eyes'] word_counts.update(morewords) #使用update()增加计数 print(word_counts.most_common(3))
>>> ================================ RESTART ================================ >>> [('eyes', 8), ('the', 5), ('look', 4)] [('eyes', 9), ('the', 5), ('my', 4)] >>>
在底层实现中,Counter是一个字典,在元素和它们出现的次数间做了映射。
>>> word_counts Counter({'eyes': 9, 'the': 5, 'my': 4, 'look': 4, 'into': 3, 'around': 2, 'not': 2, "don't": 1, 'under': 1, 'are': 1, 'looking': 1, "you're": 1, 'you': 1, 'why': 1, 'in': 1}) >>> word_counts.most_common(3) #top_three [('eyes', 9), ('the', 5), ('my', 4)] >>> word_counts['not'] 2 >>> word_counts['eyes'] 9 >>> word_counts['eyes']+1 10 >>> word_counts Counter({'eyes': 9, 'the': 5, 'my': 4, 'look': 4, 'into': 3, 'around': 2, 'not': 2, "don't": 1, 'under': 1, 'are': 1, 'looking': 1, "you're": 1, 'you': 1, 'why': 1, 'in': 1}) >>> word_counts['eyes']=word_counts['eyes']+1 #手动增加元素计数 >>> word_counts Counter({'eyes': 10, 'the': 5, 'my': 4, 'look': 4, 'into': 3, 'around': 2, 'not': 2, "don't": 1, 'under': 1, 'are': 1, 'looking': 1, "you're": 1, 'you': 1, 'why': 1, 'in': 1}) >>>
增加元素出现次数可以通过手动进行增加,也可以借助update()方法;
另外,Counter对象另一个特性是它们可以同各种数学运算操作结合起来使用:
>>> a=Counter(words) >>> a Counter({'eyes': 8, 'the': 5, 'look': 4, 'my': 3, 'into': 3, 'around': 2, 'under': 1, "you're": 1, 'not': 1, "don't": 1}) >>> b=Counter(morewords) >>> b Counter({'not': 1, 'my': 1, 'in': 1, 'you': 1, 'looking': 1, 'are': 1, 'eyes': 1, 'why': 1}) >>> c=a+b >>> c Counter({'eyes': 9, 'the': 5, 'my': 4, 'look': 4, 'into': 3, 'around': 2, 'not': 2, "don't": 1, 'under': 1, 'are': 1, 'looking': 1, "you're": 1, 'you': 1, 'in': 1, 'why': 1}) >>> # substract counts >>> d=a-b >>> d Counter({'eyes': 7, 'the': 5, 'look': 4, 'into': 3, 'my': 2, 'around': 2, 'under': 1, "you're": 1, "don't": 1}) >>>
当面对任何需要对数据制表或计数的问题时,Counter对象都是你手边的得力工具。比起利用字典自己手写算法,更应采用该方式完成任务。
相关文章
- 羊了个羊,但是Python简(li)单(pu)版
- python中矩阵的转置怎么写_Python 矩阵转置的几种方法小结
- Python批量爬取淘宝商品评价信息
- Python ---- 算法入门(2)分治算法解决【找数组的最大值和最小值】问题
- python jieba库_Python jieba库的使用说明「建议收藏」
- python中dtype的使用规范_Python numpy.dtype() 使用实例
- python爬虫—–Python访问http的几种方式「建议收藏」
- 怎么用python打开csv文件_Python文本处理之csv-csv文件怎么打开[通俗易懂]
- 迭代器Python_python进阶路线
- OpenCV-Python学习(14)—— OpenCV 绘制箭头线(cv.arrowedLine)
- python pkl文件_Python字符串格式化输出的方式包括
- 【Python】Jupyter在PyCharm中的使用
- lambda表达式python_Python中的Lambda表达式「建议收藏」
- python win32api sendmessage_Python win32api.SendMessage方法代码示例[通俗易懂]
- python hexdump_笨办法学 Python · 续 练习 26:`hexdump`
- python中的if语句格式_python if判断
- 正则表达式Python_python正则表达式匹配字符串
- 用Python进行边缘检测
- Python用KShape对时间序列进行聚类和肘方法确定最优聚类数k可视化|附代码数据
- Python 中的默认值是什么?
- 如何在 Python 中创建静态类数据和静态类方法?
- python-Python与PostgreSQL数据库-处理PostgreSQL查询结果
- 检查网站可用性的Python代码详解编程语言
- Python学习:6.python内置函数详解编程语言
- python工具——Scalene详解编程语言
- 使用Python连接SQL Server数据库(python连接sqlserver)
- 在Python中简单调用MySQL(python调用mysql)
- 在Linux系统中使用Python编程语言(linux中python)
- python解析xml文件实例分享