Details of Python list derivation
- 2021-12-11 18:36:07
- OfStack
1. List generation formula
The list generation formula is
List Comprehensions
Yes
Python
Built-in is very simple but powerful and can be used to create
list
Gets or sets the generation formula of.
The grammatical structure is as follows:
# General generative formula
[ Expression for Variable in Old list ]
# Generating formula with added conditions
[ Expression for Variable in Old list if Condition ]
# if...else Generating formula of condition
[ Expression if Condition 1 else Condition 2for Variable in Old list ]
1.1 Cases
Filter out names with noun letters less than 4. The example code is as follows:
names = ["Tom", "Lily", "Jack", "Steven", "Bod"]
# In the process of not using list generation
news_names = []
for name in names:
if len(name) > 3:
news_names.append(name)
print(news_names) # ['Lily', 'Jack', 'Steven']
# Using list generators
new_names = [name for name in names if len(name) > 3]
print(new_names) # ['Lily', 'Jack', 'Steven']
Obviously, using list generators can save many lines of code
1-100 integers that can be divisible by 3 and 5 to form a new list. Sample code, the sample code is as follows:
# Will 1-100 The integer of can be 3 And 5 Divided numbers, composition 1 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 formula
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]
Combine odd numbers from 0 to 10 and even numbers from 0 to 5 into a non-repeating list. The sample code is as follows:
# Will 0 To 10 Odd sum of 0 To 5 Even-numbered composition of 1 A list that does not repeat
# 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], [3, 4], [5, 0], [5, 2], [5, 4], [7, 0], [7, 2], [7, 4], [9, 0], [9, 2],[9, 4]]
Also supports multiple
for
Statement, if the original method is used, the hierarchy is too deep, and it is solved by using Generator 1 line
Add 200 for numbers greater than 8000 and 500 for numbers less than or equal to 8000 in the list. The example code is as follows:
# Make the number in the list greater than 8000 Addition of 200 Less than or equal to 8000 Addition of 500
number = [5000, 10000, 4500, 80000, 12000]
# Old method
for i in number:
if i > 8000:
i += 200
else:
i += 500
print(number) # [5000, 10000, 4500, 80000, 12000]
# New method
new_number = [i + 200 if i > 8000 else i + 500 for i in number]
print(new_number) # [5000, 10000, 4500, 80000, 12000]
2. Set generation formula
The syntax structure is as follows:
# General generative formula
{ Expression for Variable in Old list }
# Generating formula with added conditions
{ Expression for Variable in Old list if Condition }
# if...else Generating formula of condition
{ Expression if Condition 1 else Condition 2for Variable in Old list }
The syntax structure is basically the same as that of list generation, but because the collection is not allowed to be duplicated, all the results are automatically deduplicated
3. Dictionary generation formula
The grammatical structure of dictionary generation is different from that of set generation and list generation. The only difference is that dictionaries store information in the form of key-value pairs. In the following example, we interchange key-value in dict.
The sample code is as follows:
# Interchange key-value pairs of dictionaries
dict1 = {"a": "A", "b": "B", "c": "C"}
# On the method
new_dict1 = {}
for key, value in dict1.items(): # Return 1 Tuples for key-value pairs
new_dict1[value] = key
print(new_dict1) # {'A': 'a', 'B': 'b', 'C': 'c'}
# New method
news_dict1 = {value: key for key, value in dict1.items()}
print(news_dict1) # {'A': 'a', 'B': 'b', 'C': 'c'}
item
Method: Returns an array of (key, value) tuples that can be traversed.