Life is short, learn Python!

Collections is the standard library for Python. It provides containers for solving specific problems. That is, lists, tuples, and dicts are too cumbersome to solve, and these problems are often encountered, so they have compiled general methods for solving them. Put it in the Collections library for people to use.

There are nine types of containers in collections, of which Counter, Defaultdict, deque, Namedtuple, and Orderdict are the most common. !

Let’s talk about the use of Counter alone today!

The purpose of Counter is to keep track of the number of occurrences of values. It is an unordered container type stored as a dictionary key-value pair, where the element is the key and its count is the value. So, we can do this by taking numbers in a dictionary.

Before using it, we need to import the library as follows:

from collections import Counter
Copy the code

So to use the Counter class, we need to instantiate a Counter class, we need to instantiate an empty Counter class.

c1 = Counter()
c1
Copy the code

The results are as follows:!

We can pass arguments in parentheses to help us implement “value counting” in different cases.

Creation of the Counter class

We can create a Counter class by passing in a string, a list, a tuple, a dictionary, etc.

1. Pass in a string
c2 = Counter("hello huang")
c2
Copy the code

The results are as follows:

2. Pass in a list
lis = [i for i in "chinese"]
c3 = Counter(lis)
c3
Copy the code

The results are as follows:

Pass in a dictionary
Import random dic = {k: random. Randint (1,4) for k in "abcdefg"} c4 = Counter(dic) c4Copy the code

The results are as follows:

4. Keyword parameters
c5 = Counter(a=7,b=8)
c5
Copy the code

The results are as follows:

The Counter class counts access to element values

This is similar to accessing a dictionary, except that instead of KeyError, 0 is returned if the accessed key does not exist.

c = Counter("hello huang")
c["h"]
Copy the code

The results are as follows:

Changes to count elements in the Counter class

Count elements in the Counter class can be added and subtracted, set operations, or deleted.

1. The addition and subtraction
(1) addition

We can either do this using the update function. You can also use the + sign to do this.

(2) the subtraction

We can either do this by using the subtract function. But the – sign is different. It should be a set operation, counting only values in set A, not in set B.

2. Set operation

The above -, is a collection operation, I for you to introduce and &, | or operation.

(1) and &

The & operation finds the smallest key value for a key that exists in both collections.

c1 = Counter("chinese")
c2 = Counter("where")

c1 & c2
Copy the code

The results are as follows:

(2) | or

| operation, is to find the keys of the two largest collection of key (can be only one collection has the key, but the results of the key value must be greater than zero, less than zero is abandoned).

c1 = Counter("chinese")
c2 = Counter("where")

c1 | c2
Copy the code

The results are as follows:

3. Delete the vm

The del function is used to delete elements.

c = Counter("where")

del c["r"]
c
Copy the code

The results are as follows:

Other common functions in the Counter class

1. Most_common (n) Finds the first N numbers that repeat the most times.
c = Counter("aabbbcccddddeeeee")
c.most_common(2)
Copy the code

The results are as follows:

2. Key and value are used to get the Collections key and key values collection.
c = Counter("chinese")
c.keys()
c.values()
Copy the code

The results are as follows:

We can also use dict() to turn the contents of a collection into a dictionary. We can also use list() to turn the keys of a collection into a list.

Python is a very diverse and well-developed language, so there are definitely a lot of features THAT I haven’t considered, so if you know of any, let me know in the comments section