- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
>>> class Potato(object):
... def __getslice__(self, start, stop):
... print start, stop
...
>>> sys.maxint
9223372036854775807
>>> x = sys.maxint + 69
>>> print x
9223372036854775876
>>> Potato()[123:x]
123 9223372036854775807
为什么对 getslice 的调用不遵守我发送的 stop
,而是默默地替换 2^63 - 1?这是否意味着为您自己的语法实现 __getslice__
通常对 long 不安全?
无论如何,我可以用 __getitem__
做任何我需要的事情,我只是想知道为什么 __getslice__
显然坏了。
编辑: CPython 中截断切片的代码在哪里?这是 python(语言)规范的一部分还是只是 cpython(实现)的“功能”?
最佳答案
为实现 sq_slice
插槽的对象处理切片的 Python C 代码无法处理超过 Py_ssize_t
的任何整数 (== sys.maxsize
) . sq_slice
槽是 C-API 等同于 __getslice__
特殊方法。
对于二元素切片,Python 2 使用 SLICE+*
opcodes 之一;然后由 apply_slice()
function 处理。这使用 _PyEval_SliceIndex
function 将 Python 索引对象(int
、long
或任何实现 __index__
method 的对象)转换为 Py_ssize_t
整数。该方法具有以下注释:
/* Extract a slice index from a PyInt or PyLong or an object with the
nb_index slot defined, and store in *pi.
Silently reduce values larger than PY_SSIZE_T_MAX to PY_SSIZE_T_MAX,
and silently boost values less than -PY_SSIZE_T_MAX-1 to -PY_SSIZE_T_MAX-1.
Return 0 on error, 1 on success.
*/
这意味着当 sq_slice
提供插槽。
使用三值形式 (item[start:stop:stride]
) 的切片使用 BUILD_SLICE
opcode 代替(后跟 BINARY_SUBSCR
),这代替创建 slice()
object 而不限于 sys。最大尺寸
。
如果对象没有实现 sq_slice()
槽(因此没有 __getslice__
存在),apply_slice()
函数也会失效回到使用 slice()
对象。
至于这是实现细节还是语言的一部分:Slicings expression documentation 区分simple_slicing
和extended_slicing
;前者只允许 short_slice
形式。对于简单切片,索引必须是普通整数:
The lower and upper bound expressions, if present, must evaluate to plain integers; defaults are zero and the
sys.maxint
, respectively.
这表明 Python 2 语言 将索引限制为sys.maxint
值,不允许使用长整数。在 Python 3 中,简单切片已从语言中完全删除。
如果您的代码必须支持使用超过sys.maxsize
的值进行切片并且您必须从实现__getslice__
的类型继承,那么您的选项是:
使用三值语法,步长为None
:
Potato()[123:x:None]
显式创建 slice()
对象:
Potato()[slice(123, x)]
slice()
对象可以很好地处理 long
整数;然而 slice.indices()
method 仍然无法处理超过 sys.maxsize
的长度:
>>> import sys
>>> s = slice(0, sys.maxsize + 1)
>>> s
slice(0, 9223372036854775808L, None)
>>> s.stop
9223372036854775808L
>>> s.indices(sys.maxsize + 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: cannot fit 'long' into an index-sized integer
关于python - 切片端点被无形地截断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25597815/
简而言之: 我怎样才能切片?也就是说,能够指定我想从多个索引(例如 y = x[(2, 5, 11)] )中提取,而不仅仅是单个索引(例如 y = x[2] )。 简单示例 : 说我有这个数据: d
是否可以在 F# 中对 Array2D 进行切片? 说,let tmp =Array2D.init 100 100 (fun x y -> x * 100 + y) 如何从 tmp 中检索某些列或某些
例如,我希望文本仅显示“此处”,但它不起作用。文本经常变化,但我需要的单词保持在固定位置。我想访问该词。 我做错了什么? function myFunction() { var x = doc
当尝试使用spring的分页或切片来迭代非常大的mongodb集合时,程序运行正常,但在某些时候下一页/切片为空,并且在调试时出现“包含未知实例的页面/切片”消息. 这是代码示例: do { Pa
有人能给我一个关于如何分割 ListView 的例子吗?我正在使用 SimpleCursorAdapter 在 ListView 中显示数据.. 我的代码是这样的。 private WordDbAda
这个问题在这里已经有了答案: C++ slicing causing leak / undefined behavior / crash (3 个答案) 关闭 8 年前。 如果我有如下代码: cla
这个问题在这里已经有了答案: Understanding slicing (38 个答案) 关闭 5 年前。 我目前有 500 行数据。我想使用前五十行,然后跳过 50 行,依此类推。我该如何继续这
为什么对一行或一列进行切片会产生“无量纲数组”?例如: import numpy as np arr = np.zeros((10,10)) print arr.shape # (10, 10) 但是
我有以下 pandas 数据框: Shortcut_Dimension_4_Code Stage_Code 10225003 2 8225003
如何通过数组为 ruby 中的散列创建切片,如下所示: info = { :key1 => "Lorem", :key2 => "something...", :key3 => "
这个问题在这里已经有了答案: regex to get all text outside of brackets (4 个答案) 关闭 5 年前。 我正在编写的这个程序接收到一个大小不同的字符串,其
我尝试使用 tf.Tensor.getitem 对张量进行切片功能如下: indices = [0, 5] data[:,:,indices] 但是我得到以下错误: TypeError: can on
这个问题在这里已经有了答案: Can I create a "view" on a Python list? (10 个答案) 关闭 7 年前。 有没有一种方法可以在 Python 3 中创建序列的
我想弄清楚如何从多维数组中获取单个维度(为了论证,假设它是二维的),我有一个多维数组: double[,] d = new double[,] { { 1, 2, 3, 4, 5 }, { 5, 4,
我有一个 std::vector。我想创建代表该 vector 切片的迭代器。我该怎么做?在伪 C++ 中: class InterestingType; void doSomething(slice
写在前面 前面的文章介绍了Go的一些基本类型,本文开始涉及Go的一些容器类型,它们都是可以包含多个元素的数据结构,如数组、切片、map 数组 数组是具有相同类型且长度固定的一组元素集合,定义的格式:v
给定一个 numpy 数组和一个 __getitem__ 类型的索引,是否有一种惯用的方法来获取数组的相应切片,总是返回一个数组而不是标量? 有效索引的示例包括:int、slice、省略号或上述的元组
我创建了一个继承自 pandas.DataFrame 的类。在此类中添加了元数据(不是添加到列中,而是添加到类实例中): class MeasurementPoint(pandas.DataFrame
我想在空间上剪切视频以生成 N x M 个文件。 例如,我想把 test.video 拆分成 NxM 的瓦片? Video tiles 最佳答案 您可以使用 ffmpeg 及其 crop filter
这是一个示例代码。比如我想拉德国 在页面加载时切片。在这段代码中,它拉取第一个切片。 无功图; var 传说; var chartData = [{ 国家:“立陶宛”, 值:260}, { 国家:“爱
我是一名优秀的程序员,十分优秀!