Python cookbook(資料結構與演算法)找出序列中出現次數最多的元素演算法樣本,pythoncookbook

來源:互聯網
上載者:User

Python cookbook(資料結構與演算法)找出序列中出現次數最多的元素演算法樣本,pythoncookbook

本文執行個體講述了Python找出序列中出現次數最多的元素。分享給大家供大家參考,具體如下:

問題:找出一個元素序列中出現次數最多的元素是什麼

解決方案:collections模組中的Counter類正是為此類問題所設計的。它的一個非常方便的most_common()方法直接告訴你答案。

# Determine the most common words in a listwords = [  '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 Counterword_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 wordsmorewords = ['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_countsCounter({'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']+110>>> word_countsCounter({'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_countsCounter({'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)>>> aCounter({'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)>>> bCounter({'not': 1, 'my': 1, 'in': 1, 'you': 1, 'looking': 1, 'are': 1, 'eyes': 1, 'why': 1})>>> c=a+b>>> cCounter({'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>>> dCounter({'eyes': 7, 'the': 5, 'look': 4, 'into': 3, 'my': 2, 'around': 2, 'under': 1, "you're": 1, "don't": 1})>>>

當面對任何需要對資料製表或計數的問題時,Counter對象都是你手邊的得力工具。比起利用字典自己手寫演算法,更應採用該方式完成任務。

(代碼摘自《Python Cookbook》)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.