Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python-specific syntax deduction

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report