The paper contains 2285 words and is expected to last 7 minutes

Source: Pexels

In life, there are often some people, obviously very capable, but not by the people around.


Is the gold can always shine, this sentence in real life, in fact, is not completely applicable, a large population, there is no lack of talented people, want to succeed need a blockbuster opportunity.


Python also.


There are many libraries in Python that perform well. Collections is one of them.


The Collections module provides “high-performance container data types” and is a perfect substitute for generic container dictionaries, lists, collections, and tuples.

But it is often ignored or looked down upon.


Today, Microchip will try to be a “good player”, concise introduction to the underdog Python libraries, and discover their infinite potential.


I’ll walk you through three of these data types in detail in this article, leaving you wondering how you ever managed to program without these libraries.

Source: Pexels

Counter

Counter is aptly named — its main function is counting. It sounds simple enough, but it turns out that data scientists often have to count, so it works.


There are several ways to do this, but I usually set a list of values as shown below

from collections import Counter

ages = [22, 22, 25, 25, 30, 24, 26, 24, 35, 45, 52, 22, 22, 22, 25, 16, 11, 15, 40, 30]

value_counts = Counter(ages)

print(value_counts.most_common())

Counter.py Hosted with ❤ by GitHub

If the reader were to run the above code (this productivity tool is recommended), he would get the following output:

[(22, 5), (25, 3), (24, 2), (30, 2), (35, 1), (40, 1), (11, 1), (45, 1), (15, 1), (16, 1), (52, 1), (26, 1)]

A list of tuples in the most common order, with values first, then counts. So you can quickly see that 22 is the most common age, 5 times, and age only comes up once. Done!


DefaultDict


This is one of my favorites. DefaultDict is a versatile dictionary initialized with default values for each key when it is first encountered. Here’s an example

From collections import defaultdict

my_default_dict = defaultdict(int)

for letter in’the red fox ran as fast as it could’:

my_default_dict[letter] +=1

print(my_default_dict)


Defaultdict.py Hosted with ❤ by GitHub

return

defaultdict(<type ‘int’>,{‘a’: 4, ‘ ‘: 8, ‘c’: 1, ‘e’: 2, ‘d’: 2, ‘f’: 2, ‘i’: 1, ‘h’: 1, ‘l’: 1, ‘o’:2, ‘n’: 1, ‘s’: 3, ‘r’: 2, ‘u’: 1, ‘t’: 3, ‘x’: 1})

Typically, when you try to access a value that is not in the dictionary, you get an error screen. There are other ways to handle this, but it will add some extra code when the user wants to use the default values. In the above example, defauldict is initialized with an int, which means that it is assumed to be 0 on the first access, so it is easy to count all characters, succinctly. Another common initialization is a list, which allows users to start adding value immediately upon their first access.


NamedTuple


NamedTuple is crucial for data scientists. This next scenario may sound familiar, but because you like lists, you’re doing feature engineering, and you just need to add these features to the corresponding classes and then input them into the machine learning model. When you get hundreds of features, things get really messy. There is some ambiguity about which feature is used or which index in the list is referenced. Worse, when other people look at the code, they don’t know what to do with the list of features.


Type NamedTuples to solve this dilemma.


With just a few more lines of code, the cluttered list can be immediately restored to order. As shown in the figure below

from collections import namedtuple

Features = namedtuple(‘Features’, [‘age’, ‘gender’, ‘name’])

row = Features(age=22, gender=’male’, name=’Alex’)

print(row.age)

Namedtupl.py Hosted with ❤ by GitHub

If you run this code, it prints the word “22,” the age of the feature user stored in the row. It’s incredible! Instead of using indexes for access, you now have easy-to-understand names, which greatly improves maintainability and cleanliness of your code.


All of these features make for cleaner code.

Source: Pexels

This should give you a sense of the Collections library and some of its cool features, so get started!


You’ll be surprised at how many hidden uses they have, and the qualitative changes they can make to your code.


Enjoy the convenience they bring!


Use your little hands and let’s have a try

Leave a comment like follow

We share the dry goods of AI learning and development. Welcome to pay attention to the “core reading technology” of AI vertical we-media on the whole platform.



(Add wechat: DXSXBB, join readers’ circle and discuss the freshest artificial intelligence technology.)