Some of my favorite Python code, tips

Python is easy to learn and difficult to master. Everyone should develop the habit of recording. Good code should be recorded and learned to summarize. Frequent flipping will bring benefits! Today, I'm going to share some code structures that I've recorded myself. Welcome to Paizhuan!

01.

concise expression

Comments: Python is famous for its simplicity and efficiency, because its syntax is very simple, and it has many built-in powerful data structures:

  • For example, we can use a large number of derivation lists to generate a lot of concise code

  • For example, we can use the if else combination, which originally required 2-3 lines of code to write, and it was done in one line!

02. Sort

I ignored a line that came to mind, "Where there are people, there are rivers and lakes" , then there are data structures such as arrays that must involve sorting, taking the maximum value and taking the minimum value.

Comments: This heapq library is very easy to use, especially when we are taking the header data of some lists, such as the largest number and the smallest number, which are often used, a very practical trick! Don't say anything, hurry up!

03. Inquiry

Sorting and querying are good friends . In long data structures (dictionaries, lists), we must have query and filtering requirements. Sometimes, we need to find a certain element or a certain type of element from a very long list. What to do, it is very simple, use the advanced function filter:

1). Use lambda to filter with filter

Comments: lambda is a very concise function expression, short and concise, and used with filter, it is very beautiful. For example, we use the startswith built-in function in the string, which is very convenient to filter out the data we need in the list! (Python3 changes it a little and adds a list to the filter, otherwise the iterator address is generated)

Comments: Regular is a very good filtering method. Sometimes a good regular has dozens of lines of code. Proficient in regular data analysis and data cleaning is a necessary skill!

04.

Flatten the list

Sometimes we will encounter complex data structures, such as nesting lists inside lists, nesting layers by layers , which is very troublesome. There are several ways to flatten:

1). The first traditional method

Comments: This is solved by recursion. The idea is very simple and clear, but recursion must have an exit, and attention should be paid when designing.

2). The following two are master writing methods:

.

Comments: These two tricks seem very simple, but they are actually very complicated to understand. Many skills are combined in one line. Anyway, I can accept the second one.

05.

Conditional derivation list

Deriving a list should be my favorite Pythonic way . There are many ways to evolve it. These are all very common. Read it a few times and memorize it!

06.

Nice way to add dictionaries

When designing data structures, dictionaries are a must! Many times we will use the method with the following dictionary update, of course, the defaultdict in the collections module is better!

Comments: dict.update is relatively approachable. The first time I saw the usage of dict(dict,**options), I was a little stunned.

There is also a piece of code that has not been shared, that is, class-related code ! Due to space limitations, we will write a separate article later. There are too many class skills and good code, because the class itself involves two aspects:

  • One inward : For example, there are a large number of Rubik's cube functions and built-in functions in Python. There are many techniques in it. For different application scenarios, there are some concise usages.

  • One out : the design of a class must involve inheritance and composition, so it will definitely involve the famous design pattern . A good code structure is very clear and easy to extend.

It is easy to get started with Python, but it is difficult to master it. You must take notes frequently when you encounter excellent code , and take it out from time to time. It will be of great benefit to your skill growth!

视觉+图像主题 热门原创:80行代码!用Python做一个哆来A梦分身
用Python一键生成炫酷九宫格图片,火了朋友圈
熬了一夜!我用Python做了一个网站,帮小姐姐生成漫画头像
用Python实现Instagram滤镜,变成百变女神!
有点玄乎!用Python分析《隐秘的角落》里微表情菜鸟编程大本营,现已正式上线!
接下来我们将会在该公众号上,为大家分享优质编程语言里趣味的干货,通俗易懂的实战案例,经验分享,让菜鸟也爱上编程。长按2秒,输入【福利】
点这里,领取一大波福利

    Guess you like

    Origin http://10.200.1.11:23101/article/api/json?id=326957426&siteId=291194637