Get the App
SLTechnology News&Howtos  ›  Development  › 

How python uses Counter to count objects when counting

Shulou Source: shulou.com Published: 2022-06-01 04:27:34 10月06日 Update

This article will explain in detail how python uses Counter to count objects when counting. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Use Counter to count objects when counting

This sounds obvious, but it is often forgotten. Counting something is a common task for most programmers, and in most cases it's not very challenging-here are several ways to do it more easily.

Python's collections class library has a built-in subclass of the dict class that is dedicated to this kind of thing:

> from collections import Counter > c = Counter (hello world) > cCounter ({l: 3, o: 2,: 1, e: 1, d: 1, h: 1, r: 1, w: 1}) > > c.most_common (2) [(l, 3), (o, 2)] about "how python uses Counter to count objects" is here. I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.

Tags: Objects articles things tasks more good practical obvious things content subclasses common situation challenge articles methods knowledge programs programmers Curry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei vpn Shulou Tech Info Xiaomi MySQL