Get the App
SLTechnology News&Howtos  ›  Development  › 

How Python counts the most elements in the hash list

Shulou Source: shulou.com Published: 2022-06-03 09:06:11 10月04日 Update

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!

Tags: Elements hashes objects sequences methods articles statistics content words times questions frequent for this not much practice most ladies dictionaries bottom manual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL Shulou Tech Info Xiaomi Docker