切片类型中的错误 [英] Bug in slice type

查看:79
本文介绍了切片类型中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Python切片类型有一个方法''indices'',据说:


此方法采用单个整数参数/长度/和

计算关于切片

对象如果应用于长度序列

项目将描述的扩展切片的信息。它返回一个由三个整数组成的元组;分别是

这些是/ start /和/ stop / indices以及/ step /或

切片的步幅长度。丢失或越界索引

的处理方式与常规切片一致。

http://docs.python.org/ref/types.html

步骤为负时行为不正确切片

包括0索引。

类BuggerAll:


def __init __(self,somelist):

self.sequence = somelist [:]


def __getitem __(self,key):

if isinstance(key,slice):

start,stop,step = key.indices(len(self.sequence))

#print''切片说开始,停止,步骤是:'',开始,

停止,步骤

返回self.sequence [开始:停止:步骤]

打印范围(10)[无:无:-2 ]

打印BuggerAll(范围(10))[无:无:-2]

以上打印:


[ 9,7,5,3,1]

[]


在__getitem中取消注释print语句__显示:


切片说开始,停止,步骤是:9 -1 -2


切片对象似乎认为-1是一个有效的独占

绑定,但当使用它实际切片时,Python将
负数解释为偏离序列高端的偏移量。


良好的开始 - 停止 - 步骤值是(9,无,-2),或(9,-11,-2),

或(-1,-11 ,-2)。后两者的优点是

与记录的返回三个

整数的行为有关。

-

--Bryan


The Python slice type has one method ''indices'', and reportedly:

This method takes a single integer argument /length/ and
computes information about the extended slice that the slice
object would describe if applied to a sequence of length
items. It returns a tuple of three integers; respectively
these are the /start/ and /stop/ indices and the /step/ or
stride length of the slice. Missing or out-of-bounds indices
are handled in a manner consistent with regular slices.

http://docs.python.org/ref/types.html
It behaves incorrectly when step is negative and the slice
includes the 0 index.
class BuggerAll:

def __init__(self, somelist):
self.sequence = somelist[:]

def __getitem__(self, key):
if isinstance(key, slice):
start, stop, step = key.indices(len(self.sequence))
# print ''Slice says start, stop, step are:'', start,
stop, step
return self.sequence[start : stop : step]
print range(10) [None : None : -2]
print BuggerAll(range(10))[None : None : -2]
The above prints:

[9, 7, 5, 3, 1]
[]

Un-commenting the print statement in __getitem__ shows:

Slice says start, stop, step are: 9 -1 -2

The slice object seems to think that -1 is a valid exclusive
bound, but when using it to actually slice, Python interprets
negative numbers as an offset from the high end of the sequence.

Good start-stop-step values are (9, None, -2), or (9, -11, -2),
or (-1, -11, -2). The later two have the advantage of being
consistend with the documented behavior of returning three
integers.
--
--Bryan

推荐答案

Bryan Olson写道:
Bryan Olson wrote:

类Bugger所有:
< br _> def __init __(self,somelist):
self.sequence = somelist [:]

def __getitem __(self,key):
if isinstance(key,slice) :
start,stop,step = key.indices(len(self.sequence))
#print''切片说开始,停止,步骤是:'',开始,
停止,步骤
返回self.sequence [开始:停止:步骤]

打印范围(10)[无:无:-2]
打印BuggerAll(范围(10))[无:无:-2]

以上版画:

[9,7,5,3,1]
[]

在__getitem__中取消注释print语句显示:

切片说开始,停止,步骤是:9 -1 -2

切片对象似乎认为-1是一个有效的独占绑定,但在使用它实际切片时,Python将负数解释为偏离序列高端的偏移量。

良好的起始 - 停止 - 步长值为(9,无,-2)或(9,-11, -2),
或(-1,-11,-2)。后两者的优势在于记录了返回三个整数的行为。

class BuggerAll:

def __init__(self, somelist):
self.sequence = somelist[:]

def __getitem__(self, key):
if isinstance(key, slice):
start, stop, step = key.indices(len(self.sequence))
# print ''Slice says start, stop, step are:'', start,
stop, step
return self.sequence[start : stop : step]
print range(10) [None : None : -2]
print BuggerAll(range(10))[None : None : -2]

The above prints:

[9, 7, 5, 3, 1]
[]

Un-commenting the print statement in __getitem__ shows:

Slice says start, stop, step are: 9 -1 -2

The slice object seems to think that -1 is a valid exclusive
bound, but when using it to actually slice, Python interprets
negative numbers as an offset from the high end of the sequence.

Good start-stop-step values are (9, None, -2), or (9, -11, -2),
or (-1, -11, -2). The later two have the advantage of being
consistend with the documented behavior of returning three
integers.




我怀疑这是有原因的它是这样做的,但我同意你的意见

这看起来很奇怪。你有没有提交Sourceforge的bug报告?


BTW,同样现象的一个简单例子是:


py>范围(10)[切片(无,无,-2)]

[9,7,5,3,1]

py>切片(无,无,-2)。指示(10)

(9,-1,-2)

py>范围(10)[9:-1:-2]

[]


STeVe



I suspect there''s a reason that it''s done this way, but I agree with you
that this seems strange. Have you filed a bug report on Sourceforge?

BTW, a simpler example of the same phenomenon is:

py> range(10)[slice(None, None, -2)]
[9, 7, 5, 3, 1]
py> slice(None, None, -2).indices(10)
(9, -1, -2)
py> range(10)[9:-1:-2]
[]

STeVe


Steven Bethard写道:
Steven Bethard wrote:
我怀疑它是这样做的原因,但我同意你的意见,这似乎很奇怪。您是否在Sourceforge上提交了错误报告?


我认为切片类是年轻的,所以我的猜测是bug。我提交了报告 - 我的第一个Sourceforge错误报告。

BTW,同样现象的一个更简单的例子是:

py>范围(10)[切片(无,无,-2)]
[9,7,5,3,1]
py>切片(无,无,-2)。指示(10)
(9,-1,-2)
py>范围(10)[9:-1:-2]
[]
I suspect there''s a reason that it''s done this way, but I agree with you
that this seems strange. Have you filed a bug report on Sourceforge?
I gather that the slice class is young, so my guess is bug. I
filed the report -- my first Sourceforge bug report.
BTW, a simpler example of the same phenomenon is:

py> range(10)[slice(None, None, -2)]
[9, 7, 5, 3, 1]
py> slice(None, None, -2).indices(10)
(9, -1, -2)
py> range(10)[9:-1:-2]
[]




啊,谢谢。

-

--Bryan



Ah, thanks.
--
--Bryan


Steven Bethard写道:
Steven Bethard wrote:
Bryan Olson写道:
Bryan Olson wrote:

类Bugger所有:

def __init __(self,somelist):
self.sequence = somelist [:]

def __getitem __(self ,键):
如果isinstance(键,切片):
start,stop,step = key.indices(len(self.sequence))
#print''切片说开始,停止,步骤是:'',开始,
停止,步骤
返回self.sequence [开始:停止:步骤]

打印范围(10)[无:无: - 2]
打印BuggerAll(范围(10))[无:无:-2]

以上打印:

[9,7,5,3 ,1]

在__getitem__中取消注释print语句显示:

Slice说start,st op,step是:9 -1 -2

切片对象似乎认为-1是一个有效的独占绑定,但当使用它实际切片时,Python解释
负数作为序列高端的偏移量。

良好的起始 - 停止 - 步长值为(9,无,-2)或(9,-11,-2),
或(-1,-11,-2)。后两者的优势在于它与记录的返回三个整数的行为有关。

class BuggerAll:

def __init__(self, somelist):
self.sequence = somelist[:]

def __getitem__(self, key):
if isinstance(key, slice):
start, stop, step = key.indices(len(self.sequence))
# print ''Slice says start, stop, step are:'', start,
stop, step
return self.sequence[start : stop : step]
print range(10) [None : None : -2]
print BuggerAll(range(10))[None : None : -2]

The above prints:

[9, 7, 5, 3, 1]
[]

Un-commenting the print statement in __getitem__ shows:

Slice says start, stop, step are: 9 -1 -2

The slice object seems to think that -1 is a valid exclusive
bound, but when using it to actually slice, Python interprets
negative numbers as an offset from the high end of the sequence.

Good start-stop-step values are (9, None, -2), or (9, -11, -2),
or (-1, -11, -2). The later two have the advantage of being
consistend with the documented behavior of returning three
integers.



我怀疑它的原因是它的'这样做,但我同意你的意见,这似乎很奇怪。你有没有提交过关于Sourceforge的错误报告?

BTW,同样现象的一个更简单的例子是:

py>范围(10)[切片(无,无,-2)]
[9,7,5,3,1]
py>切片(无,无,-2)。指示(10)
(9,-1,-2)
py>范围(10)[9:-1:-2]
[]


I suspect there''s a reason that it''s done this way, but I agree with you
that this seems strange. Have you filed a bug report on Sourceforge?

BTW, a simpler example of the same phenomenon is:

py> range(10)[slice(None, None, -2)]
[9, 7, 5, 3, 1]
py> slice(None, None, -2).indices(10)
(9, -1, -2)
py> range(10)[9:-1:-2]
[]


rt = range(10)
rt [slice(None,None,-2)]
[9,7,5,3,1] rt [:: - 2]
[ 9,7,5,3,1] slice(None,None,-2).indices(10)
(9,-1,-2)[rt [x] for x in range(9, - 1,-2)]
[9,7,5,3,1]
rt = range(10)
rt[slice(None, None, -2)] [9, 7, 5, 3, 1] rt[::-2] [9, 7, 5, 3, 1] slice(None, None, -2).indices(10) (9, -1, -2) [rt[x] for x in range(9, -1, -2)] [9, 7, 5, 3, 1]




对我来说很好看。 index已返回可用(开始,停止,步骤)。

也许文档需要扩展。



Looks good to me. indices has returned a usable (start, stop, step).
Maybe the docs need expanding.


这篇关于切片类型中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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