Practical tips to write Python code concisely like this

To judge the level of a programmer, it is not only based on the amount of his hair, nor the amount of code, but also the ideas and the quality of his code. The ideas contained in the code are mainly reflected in the application of various design patterns, and the quality of the code must not only meet the requirements, but also ensure the simplicity and elegance of the code. Ensuring code quality requires long-term accumulation, developing good programming habits, and constantly thinking about optimization.

Today, I will introduce you to a practical martial art to ensure the simplicity of the code - the four types of Python derivation.
what is derivation

Deductive comprehensions (also known as analytical expressions) are a unique feature of python. A derivation is that a new sequence of data can be constructed from one sequence of data.

Seeing that the definition is very abstract, let's take a look at specific examples and learn these four derivations through examples.
list comprehension

Let's first look at a requirement:

Quickly create a list with squares of elements 1-9

Faced with this requirement, our usual implementation is as follows:

lis = for i in range(1, 10): lis.append(i*i) print(lis) # [1, 4, 9, 16, 25, 36, 49, 64, 81]

How to do it with list comprehension? You only need one line of code:

lis = [x * x for x in range(1, 10)] print(list) # [1, 4, 9, 16, 25, 36, 49, 64, 81]

This is the derivation. We can see that the syntax of the column comprehension is this:

variablename = [expression for variable in list]

Here's a more complicated one:

list = [x * y for x in range(1, 10) for y in range(1, 10)] print(lis) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 2, 4, 6, 8, 10, 12, 14, 16, 18, 3, 6, 9, 12, 15, 18, 21, 24, 27, 4, 8, 12, 16, 20, 24, 28, 32, 36, 5, 10, 15, 20, 25, 30, 35, 40, 45, 6, 12, 18, 24, 30, 36, 42, 48, 54, 7, 14, 21, 28, 35, 42, 49, 56, 63, 8, 16, 24, 32, 40, 48, 56, 64, 72, 9, 18, 27, 36, 45, 54, 63, 72, 81]

This is the type of multiple variables, so we can extend the above syntax:

variable name = [expression for variable in list for variable in xxx]

Of course, there is also a conditional column comprehension whose syntax is as follows:

variable name = [expression for variable in list if condition]

Let's look at a requirement: quickly create a list of all even numbers between 1-10.

lis = [i for i in range(1, 11) if i % 2 == 0] print(lis) # [2, 4, 6, 8, 10]
Dictionary comprehension

The form of dictionary comprehension is similar to that of list comprehension, and the syntax is similar, except that the result returned by dictionary comprehension is a dictionary.

variable name = {key: value expression}

Let's look at an example:

dic = {x: x/2 for x in range(1,11) if x % 2 == 0} print(dic) # {2: 1.0, 4: 2.0, 6: 3.0, 8: 4.0, 10: 5.0}

If I write it in the following way, what is the result returned?

dic = {‘half’: x/2 for x in range(1,11) if x % 2 == 0} print(dic)

You can try it yourself and see if the result is the same as you think.
set comprehension

After reading the first two comprehensions, you must already know how to write set comprehensions. The syntax is as follows:

variable name = {expression for variable in list for variable in xxx}

or

variable name = {expression for variable in list if condition}

For a practical example: create a collection that stores 10 even numbers.

set1 = {x for x in range(10) if x % 2 == 0} print(set1) # {0, 2, 4, 6, 8}
tuple comprehension

Let's look at an example first:

tup=(x for x in range(1,10)) print(tup)

<generator object at 0x1101fade0>

Is it a little confusing to see this result printed?

The variable returned by the code above is actually a generator, not a tuple. In fact, there is no real tuple comprehension, we can only use a similar method to generate tuples, let's call it "pseudo-tuple comprehension" for now.

Let's improve the above example:

tup=tuple(x for x in range(1,10)) print(tup) # (1, 2, 3, 4, 5, 6, 7, 8, 9)

Just prepend a tuple to explicitly cast the type.
Summarize

Four python derivations are introduced here, which are mainly used to simplify the code of the loop and generate different data structures. Of course, starting from these basic expression syntaxes, you can also apply complex derivations. You will find its power when you use it carefully when writing code. This one-line code is not only concise and clear, but also can show off your skills in front of novices. Don't be too cool to use!

Guess you like

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