Skip to main content
 首页 » 编程设计

python之哪种序列类型不支持 "extend slicing"

2024年11月01日8zengkefu

python 引用 http://docs.python.org/2/reference/datamodel.html 说:

Some sequences also support “extended slicing” with a third “step” parameter: a[i:j:k] selects all items of a with index x where x = i + n*k, n >= 0 and i <= x < j.

那么不支持“扩展切片”的序列类型是什么?

请您参考如下方法:

如今,它是任何不想/不需要支持它的用户定义序列。所有当前的 Python 内置函数都可以;在过去 - 情况并非如此......参见http://www.python.org/dev/peps/pep-0283/你会看到...

Extended slice notation for all built-in sequences. The patch by Michael Hudson is now all checked in.

这涉及到:

http://docs.python.org/release/2.3.4/whatsnew/section-slices.html

Ever since Python 1.4, the slicing syntax has supported an optional third step'' orstride'' argument. For example, these are all legal Python syntax: L[1:10:2], L[:-1:1], L[::-1]. This was added to Python at the request of the developers of Numerical Python, which uses the third argument extensively. However, Python's built-in list, tuple, and string sequence types have never supported this feature, raising a TypeError if you tried it. Michael Hudson contributed a patch to fix this shortcoming.