Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Hello everyone, I am a bowl week, a front end that does not want to be drunk (inrolled). If I am lucky enough to write an article that you like, I am very lucky
List generator
List Comprehensions are a simple yet powerful Python generator that can be used to create lists.
The syntax is as follows:
[expression for variable in old list if condition] # if... If condition 1 else condition 2for variable in old listCopy the code
case
- Example code for filtering out names with nouns less than 4:
Names = ["Tom", "Lily", "Jack", "Steven", "Bod"] # in not using list generation procedure news_names = [] for name in names: if len(name) > 3: news_names.append(name) print(news_names) # ['Lily', 'Jack', New_names = [name for name in names if len(name) > 3] print(new_names) # ['Lily', 'Jack', 'Steven']Copy the code
Obviously, many lines of code can be saved by using list generation
- Make a new list of integers from 1 to 100 that are divisible by 3 and 5. The sample code is shown below:
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, 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]Copy the code
- To form a non-repeating list of odd numbers from 0 to 10 and even numbers from 0 to 5, sample code looks like this:
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 the 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], [3, 4], [5, 0], [5, 2], [5, 4), (7, 0), (7, 2), (7, 4), (9, 0), (9, 2], [9, 4]]Copy the code
Multiple for statements are also supported. The hierarchy is too deep to use the original method
- Add 200 to a list of numbers greater than 8000 and 500 to a list of numbers less than or equal to 8000 as follows:
Number = [5000, 10000, 4500, 80000, 12000] # for I in number: if I > 8000: i += 200 else: i += 500 print(number) # [5000, 10000, 4500, 80000, New_number = [I + 200 if I > 8000 else I + 500 for I in number] print(new_number) # [5000, 10000, 4500, 80000, 12000]Copy the code
Set generator
The syntax structure is as follows:
{expression for variable in old list if condition} # if... If condition 1 else condition 2for variable in old list}Copy the code
The syntax is basically the same as list generation, but because collections do not allow duplication, all the resulting results are automatically de-duplicated
Dictionary generation
Dictionary generation has the same syntax structure as set generation and list generation. The only difference is that dictionaries store information in key-value pairs. In the following example, we swap keys and values in dict, with the following code:
Dict1 = {"a": "a", "b": "b", "c": "c"} # dict1 = {"a": "b", "c": "c"} # dictdict1 = {" b", "c": "c"} # dictdict1 = {" b", "c": "c"} # dictdict1 = {} for key, value in dict1. Items (): New_dict1 [value] = key print(new_dict1) # {'A': 'A', 'B': 'B', 'C': 'C'} # key for key, value in dict1.items()} print(news_dict1) # {'A': 'a', 'B': 'b', 'C': 'c'}Copy the code
Item method: returns a traversable array of (key, value) tuples.