These little-known but useful Python library, you know how much?

Foreword

Text and images in this article from the network, only to learn, exchange, not for any commercial purposes, belongs to original author, if any questions, please contact us for treatment.

Author: read core operation

PS: If necessary Python learning materials can be added to a small partner click the link below to obtain their own

http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef

Life, there are often some people, obviously very capable, is not optimistic about the people around.

Gold is always light, this sentence in real life, is not entirely applicable to a large population, there are many talented people, we need a chance to be successful blockbuster.

Python also.

There are many ready-made Python a good performance libraries. collections is one of them.

collections module provides a "high-performance container data type" that replace generic container dictionaries, lists, and a collection of tuples.

But it is often overlooked or underestimated.

Today, the small core will strive to be a "good horses", concisely introduce those Python libraries not to be optimistic, to explore their limitless potential.

In this article we will be carefully introduced three types of data which the reader to believe that after reading this article, you must be wondering how their own programming is done before without these libraries circumstances.

Counter

Counter name is very appropriate - its main function is to count. It sounds simple, but the fact that data scientists often have to be counted, so it is very effective.

There are several ways to achieve initialization, but I typically set below a list of values ​​of

1 from  collections import Counter
2 ages = [22, 22, 25, 25, 30, 24, 26, 24, 35, 45, 52, 22, 22, 22, 25, 16, 11, 15, 40, 30]
3 value_counts  = Counter(ages)
4 print(value_counts.most_common())

 

counter.py hosted with ❤ by GitHub

If readers want to run the above code (recommended to use this highly efficient tool), you'll 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 ordered by the most common, wherein the tuple comprises a first value and then is counted. So you can quickly learn to see 22-year-old is the most common age, frequency of 5 times, as well as age, only appears once. finished!

DefaultDict

This is one of the author's favorite. DefaultDict is initialized with default values ​​versatility dictionary when you first met each key. Next is an example

1 From collections  import defaultdict
2 my_default_dict  = defaultdict(int)
3 for letter in'the red  fox ran as fast as it could':
4     my_default_dict[letter] +=1
5 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})

 

Normally, when you try to access a value is not in the dictionary, the error screen will appear. There are other ways to deal with this problem, but it will add some extra code when the user wishes to use the default values. In the example above, use int initialized defauldict, which means that at the first visit, it will be assumed to be zero, so you can easily count all characters, clear and concise. Another common initialization list, which allows users to immediately start value on the first visit.

NamedTuple

NamedTuple crucial significance for data scientists. Then this scenario may sound familiar, because I like list, so the feature works in progress, simply add these features to the corresponding class, and then enter it into the machine learning model. When get hundreds of features, the situation becomes very confusing. For the characteristics of the particular use or which index is ambiguous references list. Worse, when other people looking at the code, they face a lot of features that simply can not start.

Enter NamedTuples to solve this dilemma.

Only need to write a few lines of code, confusing list will immediately restore order. As shown below

1 from  collections import namedtuple
2 Features  = namedtuple('Features', ['age', 'gender', 'name'])
3 row =  Features(age=22, gender='male', name='Alex')
4 print(row.age)

 

namedtuple.py hosted with ❤ by GitHub

If you want to run this code, it will print the words "22", that is, the age characteristics of the user stored in the row. Incredible! Now, no need to use an index to access and replace it with some of the names easy to understand, which greatly improves the maintainability and cleanliness code.

These features help to write more concise code.

See here, the reader should have some understanding of library collections and some of its great features, and quickly use it!

You will find a lot to hide their usefulness, and it gives you the code to bring a qualitative change in surprise.

Enjoy the convenience they bring!

Move your little hand in hand, together to try ~

Guess you like

Origin www.cnblogs.com/Qqun821460695/p/11970247.html