Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)找出序列中出現(xiàn)次數(shù)最多的元素算法示例
本文實(shí)例講述了Python找出序列中出現(xiàn)次數(shù)最多的元素。分享給大家供大家參考,具體如下:
問題:找出一個(gè)元素序列中出現(xiàn)次數(shù)最多的元素是什么
解決方案:collections模塊中的Counter類正是為此類問題所設(shè)計(jì)的。它的一個(gè)非常方便的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()增加計(jì)數(shù) print(word_counts.most_common(3))
>>> ================================ RESTART ================================ >>> [('eyes', 8), ('the', 5), ('look', 4)] [('eyes', 9), ('the', 5), ('my', 4)] >>>
在底層實(shí)現(xiàn)中,Counter是一個(gè)字典,在元素和它們出現(xiàn)的次數(shù)間做了映射。
>>> 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 #手動(dòng)增加元素計(jì)數(shù) >>> 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}) >>>
增加元素出現(xiàn)次數(shù)可以通過手動(dòng)進(jìn)行增加,也可以借助update()
方法;
另外,Counter對(duì)象另一個(gè)特性是它們可以同各種數(shù)學(xué)運(yùn)算操作結(jié)合起來使用:
>>> 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}) >>>
當(dāng)面對(duì)任何需要對(duì)數(shù)據(jù)制表或計(jì)數(shù)的問題時(shí),Counter對(duì)象都是你手邊的得力工具。比起利用字典自己手寫算法,更應(yīng)采用該方式完成任務(wù)。
(代碼摘自《Python Cookbook》)
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
如何將一個(gè)CSV格式的文件分割成兩個(gè)CSV文件
這篇文章主要介紹了如何將一個(gè)CSV格式的文件分割成兩個(gè)CSV文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07python數(shù)學(xué)建模之Matplotlib?實(shí)現(xiàn)圖片繪制
這篇文章主要介紹了python數(shù)學(xué)建模之Matplotlib?實(shí)現(xiàn)圖片繪制,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07Python3 文章標(biāo)題關(guān)鍵字提取的例子
今天小編就為大家分享一篇Python3 文章標(biāo)題關(guān)鍵字提取的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08用python記錄運(yùn)行pid,并在需要時(shí)kill掉它們的實(shí)例
下面小編就為大家?guī)硪黄胮ython記錄運(yùn)行pid,并在需要時(shí)kill掉它們的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01Python經(jīng)緯度坐標(biāo)轉(zhuǎn)換為距離及角度的實(shí)現(xiàn)
這篇文章主要介紹了Python經(jīng)緯度坐標(biāo)轉(zhuǎn)換為距離及角度的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11python3.7.3版本和django2.2.3版本是否可以兼容
在本篇文章里小編給大家整理的是一篇關(guān)于python3.7.3版本和django2.2.3版本是否可以兼容的相關(guān)知識(shí)點(diǎn)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-09-09python之PyQt按鈕右鍵菜單功能的實(shí)現(xiàn)代碼
這篇文章主要介紹了python PyQt按鈕右鍵菜單功能的實(shí)現(xiàn)代碼,代碼簡單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08