Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use python-specific syntax deduction

Shulou Source: shulou.com Published: 2022-06-01 05:07:39 10月03日 Update

This article mainly explains how to use the unique grammar of python. The content of the explanation is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn how to use the unique grammar of python.

List derivation

Basic syntax:

[out_express for out_express in input_list]

Take a chestnut.

Generate a list of [0meme 1pr 4pr 9pr 16]

Before we learn the deduction, we can implement it like this

Li = [] for i in range (5): # Loop 5 times value res_i = I * I # process each value li.append (res_i) # add the processed value to the list print (li) # the list generated by printing at the end of the loop

The running results are as follows:

[0, 1, 4, 9, 16]

A simple function, write a lot of code. If you use list derivation, it's much simpler, and the code becomes easier to read.

New_list = [I for i in range (5)] print (new_list)

The running results are as follows:

[0, 1, 4, 9, 16]

When the list derivation still needs to meet certain conditions before outputting the expression, it can be achieved through the following syntax

[out_express for out_express in input_list if out_express_condition]

Take a chestnut.

Li = [2, 7, 4, 8, 8, 12, 15, 15, 3, 4, 4, 7]

Squares the elements less than 0 in the li list and saves them to the new list

Li = [2,7,4,8,-12,-9, 3,-4,-7] new_li = [I * * 2 for i in li if i < 0] print (new_li)

The running results are as follows:

[144, 81, 16, 49]

Loop nested derivation? Give me a chestnut. The generated list li is ['1a "," 1 "," 1 "," 1c "," 2a "," 2a "," 2c "," 3a "," 3b "," 3c']

Li = [I + j for i in '123' for j in' abc'] print (li)

The running results are as follows:

['1a','1b','1c','2a','2b','3a','3b','3c']

Dictionary derivation

Basic syntax:

[out_exp_key:out_exp_value for out_exp in input_list]

Take a chestnut.

Generate a dictionary

The key value of the dictionary is the element index of the list li

The value value of the dictionary is the element of the list

Li = ["age", "name", "gender"]

Li = ["age", "name", "gender"] dic = {li.index (I): i for i in li} print (dic)

The running results are as follows:

{0: 'age', 1:' name', 2: 'gender'}

And the dictionary derivation can quickly interchange the kpene v in the dictionary.

Dic1 = {0: "0", 1: "1", 2: "2"} dic2 = {v: k for k, v in dic1.items ()} print (dic2)

The running results are as follows:

{'0: 0,'1: 1,'2: 2}

Set derivation

The most important feature of ✅ collection is that it can be deduplicated automatically.

Basic syntax:

{out_exp_res for out_exp in input_set}

Take a chestnut.

Randomly generate 10 elements between 1 and 100 and remove the duplicates

Import randoms1 = {random.randint (1100) for i in range (10)} print (S1)

The running results are as follows:

{89, 37, 73, 42, 82, 87, 56, 25, 29}

Thank you for your reading, the above is the content of "how to use python-specific grammar deduction". After the study of this article, I believe you have a deeper understanding of how to use python-specific grammar deduction, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Grammar result run dictionary chestnut generation element learning loop code content that is processing maximum simplicity between but function can be passed ideas Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Shulou Tech Info Microsoft Shulou Information macOS