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 of python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of how to use python's Counter, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to use python's Counter. Let's take a look at it.

Counter

Counter is also a subclass of dict, and it has all the features of dict. As the name suggests, its main function is for statistical counting.

For example, count the number of times each word appears in a string, count the number of times each element appears in a list, and so on. Any scenario that needs to be counted can be implemented with Counter.

For example, when I want to count the number of occurrences of each word in the string "hello world", I can do this with a dictionary.

C = dict ()

> > for s in "helloworld":

... C [s] = c.setdefault (s, 0) + 1

> > c

{'hype: 1,' eBay: 1, 'lump: 3,' oval: 2, 'wicked: 1,' ritual: 1, 'dudes: 1}

If you use Counter, it's even easier, just pass the string as an argument.

> from collections import Counter

> c = Counter ("helloworld")

> > c

Counter ({'lump: 3,' oasis: 2, 'hype: 1,' eBay: 1, 'wicked: 1,' ritual: 1, 'dudes: 1})

Another practical method for Counter is Counter.most_common (n), which can return the first n elements with the most occurrence.

>

[('lame, 3)]

>

[('lumped, 3), (' oiled, 2)]

>

[(1), (3), (3), (2), (1)]

> > >

This is useful in statistical ranking, but in large applications, if the data is stored in the database, this operation can actually be implemented in the database, there is no need to use Python to deal with.

OrderedDict

OrderedDict is also a subclass of dict. It is an ordered dictionary. Before Python3.6, dictionary elements were not guaranteed to be ordered, so there was OrderedDict.

> from collections import OrderedDict

> > od = OrderedDict ()

> od ['a'] = 1

> od ['b'] = 2

> od ['c'] = 3

> od

OrderedDict ([('ajar, 1), (' baked, 2), ('cased, 3)])

The so-called order is not arranged by size, but by the order in which key is added. An is the first to join, so it is in front of the list. However, starting with Python3.7, dict itself has this feature, such as:

> > d = dict ()

> d ['a'] = 1

> d ['b'] = 2

> d ['c'] = 3

> > d

{'averse: 1,' baked: 2, 'catered: 3}

By default, it is arranged in the order in which it is inserted, so starting with Python3.7, OrderedDict is not very useful, because dict has already implemented the feature of ordering.

This is the end of the article on "how to use the Counter of python". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to use python's Counter". If you want to learn more, you are 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

Internet Technology

Wechat

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

12
Report