A slicing selects a range of items in a sequence object (e.g., a string, tuple or list). Slicings may be used as expressions or as targets in assignment or del statements. The syntax for a slicing:
一个片断选择某个有序类型对象(如, 字符串, 元组, 列表)一段范围之内的项. 片断可以作为表达式使用,或者是赋值和del语句的目标. 下面是片断的句法:
slicing |
::= | simple_slicing | extended_slicing |
simple_slicing |
::= | primary "[" short_slice "]" |
extended_slicing |
::= | primary "[" slice_list "]" |
slice_list |
::= | slice_item ("," slice_item)* [","] |
slice_item |
::= | expression | proper_slice | ellipsis |
proper_slice |
::= | short_slice | long_slice |
short_slice |
::= | [lower_bound] ":" [upper_bound] |
long_slice |
::= | short_slice ":" [stride] |
lower_bound |
::= | expression |
upper_bound |
::= | expression |
stride |
::= | expression |
ellipsis |
::= | "..." |
There is ambiguity in the formal syntax here: anything that looks like an expression list also looks like a slice list, so any subscription can be interpreted as a slicing. Rather than further complicating the syntax, this is disambiguated by defining that in this case the interpretation as a subscription takes priority over the interpretation as a slicing (this is the case if the slice list contains no proper slice nor ellipses). Similarly, when the slice list has exactly one short slice and no trailing comma, the interpretation as a simple slicing takes priority over that as an extended slicing.
在这里形式句法的说明中有点含糊: 任何看起来像表达式表的语法构件也能看作是片断表, 所以任何下标都可以解释为片断. 但这样要比更复杂的句法要合适, 该定义是没有歧义的, 在这种情况下(在片断表中没有包括适当的片断或者省略写法)优先将其解释为下标, 而不是片断. 类似地, 如果一个片断表很精确地是一个没有后跟逗号的简短片断, 则将其优先解释为简单片断,而不是扩展片断.
The semantics for a simple slicing are as follows. The primary must
evaluate to a sequence object. The lower and upper bound expressions,
if present, must evaluate to plain integers; defaults are zero and the
sys.maxint
, respectively. If either bound is negative, the
sequence's length is added to it. The slicing now selects all items
with index k such that
i <= k < j
where i
and j are the specified lower and upper bounds. This may be an
empty sequence. It is not an error if i or j lie outside the
range of valid indexes (such items don't exist so they aren't
selected).
一个简单片断的语义如下.主要是要将其导成一个有序类型对象. 如果给出了下限和上限表达式, 它们必须是普通整数; 其默认值为0或sys.maxint, 分别的, 如果其中一个界限为负数, 那么将其与该有序类型对象的长度相加得到新的界限值, 这样片断就是指出了所有的项k, 其中 i <= k < j , i和j是下限和上限.这里允许有空有序类型对象.如果i或j超出索引的合法值并不算是错误(这样的项不存在,所以不被选择).
The semantics for an extended slicing are as follows. The primary
must evaluate to a mapping object, and it is indexed with a key that
is constructed from the slice list, as follows. If the slice list
contains at least one comma, the key is a tuple containing the
conversion of the slice items; otherwise, the conversion of the lone
slice item is the key. The conversion of a slice item that is an
expression is that expression. The conversion of an ellipsis slice
item is the built-in Ellipsis
object. The conversion of a
proper slice is a slice object (see section 3.2) whose
start, stop and step attributes are the
values of the expressions given as lower bound, upper bound and
stride, respectively, substituting None
for missing
expressions.
一个扩展片断的语义如下.主要是要将其导成一个映射对象, 并且由该片断表构成的键作索引. 如果一个片断表包括至少一个逗号, 那么键就是一个包括由片断中的所有项转换而来的元组; 否则, 就用独立的片断项作转换成为键. 为表达式的片断项转换后仍是该表达式, 包括有省略写法的片断项转换后是一个内建的Ellipsis对象. 正常的片断转换后是一个start,stop和step属性为给定的下限,上限和步长的片断对象(见3.2节), 对于缺少的表达式用None替代.