# 列表理解如何工作的解释？

``a = [1,2,3,4] b = [x for x in a]` `

` `a = [[1,2],[3,4],[5,6]] b = [x for xs in a for x in xs]` `

` `[leaf for branch in tree for leaf in branch]` `

` `for branch in tree: for leaf in branch: yield leaf` `

PEP202断言这个语法的“最后一个索引变化最快”是“正确的”，特别是没有解释为什么

` ` +----------------a------------------+ | +--xs---+ , +--xs---+ , +--xs---+ | for xs in a | | x , x | | x , x | | x , x | | for x in xs a = [ [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ] b = [ x for xs in a for x in xs ] == [1,2,3,4,5,6] #a list of just the "x"s` `

`b = [x for xs in a for x in xs]`类似于下面的嵌套循环。

` `b = [] for xs in a: for x in xs: b.append(x)` `

` `...for xs in a...]` `

` `...for x in xs]` `

` `b = [] for xs in a: for x in xs: b.append(x)` `

` `result = [] for xs in a: for x in xs: result.append(x)` `

` ` ______ row 1 |1 | 2 | ______ row 2 |3 | 4 | ______ row 3 |5 | 6 | ______` `

` `b = [element for row in matrix for element in row]` `

b = [x for xs in x for xs]与[

` `b = [] for xs in a: for x in xs: b.append(x)` `