"切片"在Python表达式文档中 [英] "Slicing" in Python Expressions documentation

查看:125
本文介绍了"切片"在Python表达式文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白Python文档的以下部分:

I don't understand the following part of the Python docs:

http://docs.python.org/reference/expressions.html#slicings

这是指列表切片( x = [1,2,3,4]; x [0:2] )..?特别是引用省略号的部分。

Is this referring to list slicing ( x=[1,2,3,4]; x[0:2] )..? Particularly the parts referring to ellipsis..

slice_item       ::=  expression | proper_slice | ellipsis




作为表达式的切片项目的转换是该表达式。省略号切片项目的转换是内置的省略号对象。

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.


推荐答案

定义简单测试类,只是打印正在传递的内容:

Defining simple test class that just prints what is being passed:

>>> class TestGetitem(object):
...   def __getitem__(self, item):
...     print type(item), item
... 
>>> t = TestGetitem()

表达式示例:

>>> t[1]
<type 'int'> 1
>>> t[3-2]
<type 'int'> 1
>>> t['test']
<type 'str'> test
>>> t[t]
<class '__main__.TestGetitem'> <__main__.TestGetitem object at 0xb7e9bc4c>

切片示例:

>>> t[1:2]
<type 'slice'> slice(1, 2, None)
>>> t[1:'this':t]
<type 'slice'> slice(1, 'this', <__main__.TestGetitem object at 0xb7e9bc4c>)

省略号示例:

>>> t[...]
<type 'ellipsis'> Ellipsis

带省略号和切片的元组:

Tuple with ellipsis and slice:

>>> t[...,1:]
<type 'tuple'> (Ellipsis, slice(1, None, None))

这篇关于&QUOT;切片&QUOT;在Python表达式文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆