Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Python counts the most elements in the hash list

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you how Python statistics hash list of the most elements, I believe that most people do not understand, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!

problem

There is a sequence of elements, and I want to know which element appears most often in the sequence.

Solution

The Counter class in the collections module is transferred to the one designed by Ms. for this problem. It even has a very convenient most_common () method that can tell us the answer directly.

To illustrate usage, suppose there is a list of a series of words, and we want to find out which words appear most frequently.

Here's what we do:

Words = ['look',' into','my', 'eyes',' look',' into','my', '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)]

The discussion can provide any hashable sequence of objects to the Counter object as input. In the underlying implementation, Counter is a dictionary that maps elements to the number of times they appear. Example:

Word_counter ['not'] # 1word_counter [' eyes'] # 8

If you want to increase the count manually, you can simply increase it by yourself:

Morewords = ['why','are','you','not','looking','in','my','eyes'] for word in morewords: word_ counts [word] + = 1 print (word_counts [' eyes']) # 9

Another way is to use the update () method:

Word_counts.update (morewords)

Counter objects can also be used in combination with various mathematical operations:

> a = Counter (words) > b = Counter (morewords) > aCounter ({'eyes': 8,' the': 5, 'look': 4,' into': 3, 'my': 3,' around': 2, "you're": 1, "don't": 1, 'under': 1,' not': 1}) > > bCounter ({'eyes': 1,' looking': 1, 'are': 1,' in': 1, 'not': 1) > # Combine counts > c = a + b > > cCounter ({'eyes': 9,' the': 5, 'look': 4,' my': 4, 'into': 3,' not': 2): 2, "you're": 1, "don't": 1 in': 'in': 1' why': 1 looking up: 1 are': 1 under': 1 'you': 1}) > # Subtract counts > d = a-b > dCounter ({' eyes': 7, 'the': 5,' look': 4, 'into': 3,' my': 2, 'around': 2, "you're": 1, "don't": 1,' under': 1}) are all the contents of the article "how Python counts the most elements in the hash list" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report