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 to use the Counter class in Python

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

Share

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

This article mainly introduces the relevant knowledge of "how to use Counter class in Python". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use Counter class in Python" can help you solve the problem.

Counter class

The purpose of the Counter class is to track the number of occurrences of the value. It is an unordered container type that is stored as a dictionary key-value pair, where the element is key and its count is value. The count value can be any Interger (including 0 and negative numbers). The Counter class is very similar to bags or multisets in other languages.

Create

Counter is a container object. Its main function is to count hash objects. You can initialize it in three ways.

The parameter in the parameter can iterate the object Counter ("success")

Pass in the keyword parameter Counter ((smiles 3pr citation 2je 2m 1pm 1))

Pass in the dictionary Counter ({"s": 3, "c" = 2, "e" = 1, "u" = 1})

The following code illustrates the method that the Counter class creates:

> > c = Counter () # create an empty Counter class > c = Counter ("gallahad") # create from an iterable-capable object (list, tuple, dict, string, etc.) > c = Counter ({"a": 4, "b": 2}) # create from a dictionary object > c = Counter (aq4, bread2) # access to create count values from a set of key values and missing keys

Returns 0 instead of KeyError; when the accessed key does not exist. Otherwise, it returns its count.

> c = Counter ("abcdefgab") > c ["a"] > 2 > c ["c"] > 1 > c ["h"] > 0 counter update

You can use an iterable object or another Counter object to update the key value.

The update of the counter includes increasing and decreasing.

Add the use of update () method:

> c = Counter ("which") > c.update ("witch") # Update with another iterable object > c ["h"] > 2 > d = Counter ("watch") > c.update (d) # Update with another Counter object > c ["h"] > 3

To reduce, use the subtract () method:

> c = Counter ("which") > c.subtract ("witch") # Update with another iterable object > c ["h"] > 1 > d = Counter ("watch") > c.subtract (d) # Update with another Counter object > c ["a"] > deletion of-1 key

When the count value is 0, it does not mean that the element is deleted, and the deleted element should use del.

> > c = Counter ("abcdcba") > cCounter ({"a": 2, "c": 2, "b": 2, "d": 1}) > > c ["b"] = 0 > cCounter ({"a": 2, "c": 2, "d": 0}) > del c ["a"] > > cCounter ({"c": 2, "b": 2, "d": 1}) elements ()

Returns an iterator.

The element is contained in the iterator as many times as it is repeated. Elements are arranged in no definite order, and elements with a number less than 1 are not included.

> c = Counter (axi4, baux2, cyste0, dandrow2) > list (c.elements ()) ["a", "b"] most_common ([n])

Returns a list of TopN. If n is not specified, all elements are returned. When multiple elements have the same count value, the arrangement is in no definite order.

> c = Counter ("abracadabra") > c.most_common () [("a", 5), ("r", 2), ("b", 2), ("c", 1), ("d", 1)] > c.most_common (3) [("a", 5), ("r", 2), ("b", 2)] fromkeys

Unimplemented class methods.

Shallow copy copy

> c = Counter ("abcdcba") > cCounter ({"a": 2, "c": 2, "b": 2, "d": 1}) > > d = c.copy () > dCounter ({"a": 2, "c": 2, "b": 2, "d": 1}) arithmetic and set operation

+, -, &, and | actions can also be used for Counter. Where the & and | operations return the minimum and maximum values of each element of the two Counter objects, respectively. It is important to note that the resulting Counter object removes elements less than 1.

> c = Counter (axi3, baux1) > d = Counter (axi1, baux2) > c + d # c [x] + d [x] Counter ({"a": 4, "b": 3}) > > c-d # subtract (keep only positive counting elements) Counter ({"a": 2}) > > c & d # intersection: min (c [x], d [x]) Counter ({"a": 1) "b": 1}) > c | d # Union: max (c [x], d [x]) Counter ({"a": 3, "b": 2}) common operations

Here are some common operations of the Counter class, derived from the official Python documentation.

Sum (c.values ()) # Total number of all counts c.clear () # reset Counter object Note that instead of deleting list (c) # converting keys in c to lists set (c) # converting keys in c to setdict (c) # converting key-value pairs in c into dictionaries c.items () # into (elem, cnt) format list Counter (dict (list_of_pairs)) # from (elem) The list in cnt) format is converted to Counter class object c.most_common () [:-Nc.most_common Counter 1] # take out the n elements with the lowest count c + = Counter () # remove 0 and negative values about "how to use Counter classes in Python" ends here Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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