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 list derivation in Python

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Editor to share with you how to use the list-driven style in Python. I hope you will get something after reading this article. Let's discuss it together.

1. List generation

List generator, or List Comprehensions, is a very simple but powerful generator built into Python that can be used to create list.

The grammatical structure is as follows:

# General generative [expression for variable in Old list] # conditional Generation [expression for variable in Old list if condition] # Generation of if...else condition [expression if condition 1 else condition 2for variable in Old list] 1.1 case

Filter out names with less than 4 noun letters. The sample code is as follows:

Names = ["Tom", "Lily", "Jack", "Steven", "Bod"] # in the process news_names = [] for name in names: if len (name) > 3: news_names.append (name) print (news_names) # ['Lily',' Jack'] 'Steven'] # use list generation new_names = [name for name in names if len (name) > 3] print (new_names) # [' Lily', 'Jack',' Steven']

Obviously, using list generation can save a lot of lines of code

Form a new list of integers from 1 to 100 that are divisible by 3 and 5. Sample code, which is as follows:

# numbers that divide integers from 1 to 100 by 3 and 5 Form a new list # Old method number_list = [] for i in range (101): if I% 3 = = 0 and I% 5 = = 0: number_list.append (I) print (number_list) # [0, 15, 30, 45, 60, 75, 90] # list generation new_num_list = [i for i in range (101) if I% 3 = = 0 and I% 5 = 0] print (new_num_list) # [0,15,30 45, 60, 75, 90]

Make an unrepeatable list of odd numbers from 0 to 10 and even numbers from 0 to 5. The sample code is as follows:

# form odd numbers from 0 to 10 and even numbers from 0 to 5 into a non-repeating list # Old method news_number_list = [] for x in range (10): if x% 2! = 0: for y in range (5): if y% 2 = = 0: news_number_list.append ([x, y]) print (news_number_list) # [[1,0], [1,2], [1 4], [3,0], [3,2], [3,4], [5,0], [5,2], [5,4], [7,0], [7,2], [7,4], [9,0], [9,2], [9,4]] # New method news_num_list = [x Y] for x in range (10) if x% 2! = 0 for y in range (6) if y% 2 = = 0] print (news_num_list) # [[1,0], [1,2], [1,4], [3,0], [3,2], [5,0], [5,2], [5,4], [7,0], [7,2], [7,4] [9, 0], [9, 2], [9, 4]]

Multiple for statements are also supported. If you use the original method, the hierarchy is too deep, so use a generated one-line solution.

Add the numbers greater than 8000 in the list to those less than or equal to 8000. The sample code is as follows:

# add 500number = [5000, 10000, 4500, 80000, 12000] # the old method for i in number: if I > 8000: I + = 200else: I + = 500print (number) # [5000, 10000, 4500,80000,12000] # New method new_number = [I + 20000 if I > 8000 else I + 8000 for i in number] print (new_number) # [5000,450080000] 12000] 2. Set generation

The syntax structure is as follows:

# ordinary generator {expression for variable in old list} # conditional generation {expression for variable in old list if condition} # if...else condition {expression if condition 1 else condition 2for variable in old list}

The syntax structure is basically the same as the list generation, but because the set does not allow repetition, all the results are automatically deduplicated.

3. Dictionary generation

Dictionary generation is consistent with the grammatical structure of set generation and list generation, except that dictionaries store information in the form of key-value pairs. In the following example, we exchange keys and values in dict.

The sample code is as follows:

# swap dictionary key-value pairs dict1 = {"a": "A", "b": "B", "c": "C"} # method new_dict1 = {} for key, value in dict1.items (): # returns a tuple that stores the key-value pair [value] = keyprint (new_dict1) # {'invalid:' ajar, 'labeled:' b.' New method news_dict1 = {value: key for key, value in dict1.items ()} print (news_dict1) # {'A':'A','B': 'baked,' C':'c'}

Item method: returns a traverable (key, value) tuple array.

After reading this article, I believe you have a certain understanding of "how to use the list derivation in Python". If you want to know more about it, please follow the industry information channel. Thank you for your reading!

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