In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to use Python's Counter container", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use Python's Counter container" article.
First, initialize Counter
Counter supports three forms of initialization, such as providing an array, a dictionary, or individual key-value pair "=" assignments. The specific initialization code is as follows:
Import collectionsa = collections.Counter (['await,' baked,'c']) b = collections.Counter ({"a": 2, "b": 3, "c": 1}) c = collections.Counter (axi2, baked 3, 1) print (a) print (b) print (c)
This is populated directly through the constructor. Of course, we can also construct an empty Counter directly and populate it with the update () function.
Import collectionsa = collections.Counter () a.update (['averse,' baked,'c']) print (a) II, ergodic Counter
With the initialization and update above, there are a lot of values in our Counter container. Because the output is in the form of a dictionary, we can access it directly through the dictionary.
Import collectionsa = collections.Counter () a.update (['averse,' baked,'c']) for key in 'abcde': print (key, "=", a [key])
You can see the container Counter, and you can output null values without reporting errors. Because when we don't find a value, the default count is 0.
3. Elements ()
Of course, if you want to implement that kind of traversal with no zero value for the output. You can use the elements () iterator. The specific code is as follows:
Import collectionsa = collections.Counter () a.update ('caabbbc') print (list (a.elements ()
It is important to note that although elements () removes the value of 0, it does not guarantee the traversal order.
IV. Most_common
The most_common () function generates a sequence that contains the n longest encountered input values and the corresponding count. Here, let's count the number of letters in a document. The specific code is as follows:
Import collectionsc = collections.Counter () with open ('English document .txt', 'rt') as f: for line in f: c.update (line.rstrip (). Lower ()) for letter, count in c.most_common (5): print ("{}: {}" .format (letter, count))
Among the English documents counted here, the number of spaces is the largest, reaching 124. The other time is the largest number in the first few. In this way, we can generate the frequency distribution of letters in an English document through Counter, which is used in natural language processing. It can be perfectly combined for statistical use.
Fifth, arithmetic operation
Counter container can not only count the distribution of frequency. You can also do some arithmetic. For example, add two Counter containers for statistics, or subtract from each other. The specific operations are as follows:
Import collectionsc1 = collections.Counter ('abcbcabds') c2 = collections.Counter (' abcbcabds') print ("original value:") print (C1) print (c2) print ("after arithmetic:") print (C1 + c2) print (C1-c2) print (C1 & c2) print (C1 | c2) above is the content of this article about "how to use Counter containers in Python". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.