A list display is a possibly empty series of expressions enclosed in square brackets:
一个列表用一对方括号括住的表达式序列(可能为空)表示:
test |
::= | and_test ( "or" and_test )*
| lambda_form |
testlist |
::= | test ( "," test )* [ "," ] |
list_display |
::= | "[" [listmaker] "]" |
listmaker |
::= | expression ( list_for
| ( "," expression )* [","] ) |
list_iter |
::= | list_for | list_if |
list_for |
::= | "for" expression_list "in" testlist
[list_iter] |
list_if |
::= | "if" test [list_iter] |
A list display yields a new list object. Its contents are specified by providing either a list of expressions or a list comprehension. When a comma-separated list of expressions is supplied, its elements are evaluated from left to right and placed into the list object in that order. When a list comprehension is supplied, it consists of a single expression followed by at least one for clause and zero or more for or if clauses. In this case, the elements of the new list are those that would be produced by considering each of the for or if clauses a block, nesting from left to right, and evaluating the expression to produce a list element each time the innermost block is reached.
使用一个列表会生成一个新的列表对象.它的值由表达式表或由深列表给出.当给出一个逗号分隔的表达式表时, 从左到右地对每个元素求值然后按顺序放进列表对象中. 如果给出的是深列表, 它由至少是一个for子句以及后跟零个或多个for/if子句构成的一个表达式组成. 此时, 新列表的元素由每个for或if子句块决定, 嵌套是从左至右方向的, 而且每执行到最内部的语句块就产生一个列表元素.