来自函数式编程Haskell,可以用来动态的创建列表。
语法:
[expr for iter_var in iterable]
例:
>>>[x ** 2 for x in range(6)]
[0,1,4,9,16,25]
拓展语法:
[expr for iter_var in iterable if cond_expr]
例:
seq [11,10,9,9,10,10,9,8,23,9,7,18,12,11,12]
>>>[x for x in seq if x % 2]
[11,9,9,9,23,9,7,11]