除了最简单的列表派生和生成器表达式之外,还有字典派生、集合派生等等。
[En]
In addition to the simplest list derivation and generator expression, there are dictionary derivation, set derivation, and so on.
下面是以列表派生为例的详细派生格式,它也适用于其他派生。
[En]
The following is a detailed derivation format taking the list derivation as an example, which is also applicable to other derivations.
variable = [out_exp_res for out_exp in input_list if out_exp == 2]
out_exp_res: 列表生成元素表达式,可以是有返回值的函数。
for out_exp in input_list: 迭代input_list将out_exp传入out_exp_res表达式中。
if out_exp == 2: 根据条件过滤哪些值可以。
例一:30以内所有能被3整除的数
multiples = [i for i in range(30) if i % 3 is 0]
print(multiples)
# Output: [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]
例二:30以内所有能被3整除的数的平方