By using the List Builder, you can quickly generate a list that can derive another list from a list, while the code is very concise.
format [x for x in content][x for x in content if condition]
1: To put the generated element x in front, execution, the first to execute the later for loop
2: You can have multiple for loops followed by a For loop, or you can add an if condition after the for loop
The 3:for loop can be followed by any iterator (tuple, list, generator) in any way. ) As long as there is a value in the object that can iterate.
1: Single-loop list generation
String Generation List
print ([x for X in ' ABCD ']) #[' A ', ' B ', ' C ', ' d ']
List Generation List
print ([x for X in range (10)]) #[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
List Build list if condition
print ([x for x in range] if x > 5]) #[6, 7, 8, 9]
2: Double-loop list generation
Print ([M + N for m in ' ABC ' for n ' XYZ ') "#[' AX ', ' AY ', ' AZ ', ' BX ', ' by ', ' BZ ', ' CX ', ' CY ', ' CZ ']
See document: List Generation--Teacher Liao's Blog
Python list-generated, list-derived