切片不一致? [英] Slice inconsistency?

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

问题描述

我发现当使用负索引时,传递给

__getitem__的切片对象取决于切片的数量。一个例子澄清:


a类:

def __getitem __(自我,索引):

返回索引

I found that when using negative indices, the slice object passed to
__getitem__ depends on the number of slices. An example to clarify:

class a:
def __getitem__(self, index):
return index

b = a()
print b [: - 1]
Traceback(最近一次调用最后一次) ):

文件"< stdin>",第1行,在?

AttributeError:实例没有属性''__len__''


但是,如果我传递多个切片:

print b [: - 1,: - 1]
(切片(无,-1,无),切片(无,-1,无))


如果我们在课堂上添加以下__len__:


def __len __(自我):

返回42


然后是单身切片基于__len__工作:

打印b [: - 1]
切片(0,41,无)


但不适用于 ;多"切片:

print b [: - 1,: - 1]
(切片(无,-1,无),切片(无,-1,无))


我遇到这些问题,因为我想切片一个多维的

对象。当切片对象时,__ getitem__返回一个所需大小的数字数组



打印数据
< opendap.client.dataset instance at 0x406304ec> print data.variables [''u'']。形状
(16,17,21)u = data.variables [''u''] [:,:,:]
打印类型( u)
< type''array''> print u.shape
(16,17,21)u [0,0,0]
b = a()
print b[:-1] Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: a instance has no attribute ''__len__''

But if I pass a "multiple" slice:
print b[:-1,:-1] (slice(None, -1, None), slice(None, -1, None))

If we add the following __len__ to class a:

def __len__(self):
return 42

Then the "single" slice works based on __len__:
print b[:-1] slice(0, 41, None)

But not for the "multiple" slice:
print b[:-1,:-1] (slice(None, -1, None), slice(None, -1, None))

I am having these problems because I want to slice a multi-dimensional
object. When slicing the object, __getitem__ returns a Numeric array
of the desired size:
print data <opendap.client.dataset instance at 0x406304ec> print data.variables[''u''].shape (16, 17, 21) u = data.variables[''u''][:,:,:]
print type(u) <type ''array''> print u.shape (16, 17, 21) u[0,0,0]



-1728


在对象中使用像这样的切片有什么问题吗?一个

更好的方法?


-1728

Is there something wrong in using slices like this in objects? A
better way?

推荐答案



" Roberto A. F. De Almeida" < RO ***** @ dealmeida.net>在消息中写道

新闻:10 ************************** @ posting.google.c om ...

"Roberto A. F. De Almeida" <ro*****@dealmeida.net> wrote in message
news:10**************************@posting.google.c om...
我发现当使用负索引时,传递给
__getitem__的切片对象取决于切片的数量。一个例子澄清:

类a:
def __getitem __(self,index):
返回索引
I found that when using negative indices, the slice object passed to
__getitem__ depends on the number of slices. An example to clarify:

class a:
def __getitem__(self, index):
return index
b = a()
打印b [: - 1] Traceback(最近一次调用最后一次):
文件"< stdin>",第1行,在?
AttributeError:一个实例没有属性''__len__''

但是如果我传递一个多个切片:
print b [: - 1,: - 1](切片(无,-1,无),切片(无,-1,无))


方括号的下标(索引/键)是一个* single *对象,在这个

的情况下是一个元组。元组的内容是无关紧要的(对于这个

代码)。任何元组都会被回应:
b = a()
print b[:-1] Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: a instance has no attribute ''__len__''

But if I pass a "multiple" slice:
print b[:-1,:-1] (slice(None, -1, None), slice(None, -1, None))
A square-bracketed subscript (index/key) is a *single* object, in this
case a tuple. The contents of the tuple are irrelevant (for this
code). Any tuple will be echoed:
b [1,2,3]
b[1,2,3]



(1,2 ,3)

如果我们在课堂上添加以下__len__:

def __len __(自我):
返回42


与元组密钥的处理完全无关。

在对象中使用像这样的切片有什么问题吗?一个更好的方法?


(1, 2, 3)
If we add the following __len__ to class a:

def __len__(self):
return 42
Completely irrelevant for the processing of tuple keys.
Is there something wrong in using slices like this in objects? A
better way?




你可能想要的是b [: - 1] [: - 1]等。每个索引必须是

单独括号以访问列表(等)列表中的项目。


Terry J. Reedy



What you probably want is b[:-1][:-1], etc. Each index must be
separately bracketed to access items in list of lists (etc).

Terry J. Reedy


< a href =mailto:ro ***** @ dealmeida.net> ro ***** @ dealmeida.net (Roberto AF De Almeida)写道:
ro*****@dealmeida.net (Roberto A. F. De Almeida) writes:
我发现当使用负索引时,传递给
__getitem__的切片对象取决于切片的数量。
I found that when using negative indices, the slice object passed to
__getitem__ depends on the number of slices.




它有点像害怕我现在检查一下,但是:你是否使用Python 2.3获得了
?事情*可能*在这里发生了变化。


干杯,

mwh


-

但是因为我不是想在软件大中给任何人留下深刻印象,所以我宁愿用一根大杆,一根安全带来走线,

a net,离地不超过3英尺。

- Grant Griffin,comp.lang.python



It''s bit of a pest for me to check things right now, but: are you
using Python 2.3? Things *may* have changed here.

Cheers,
mwh

--
But since I''m not trying to impress anybody in The Software Big
Top, I''d rather walk the wire using a big pole, a safety harness,
a net, and with the wire not more than 3 feet off the ground.
-- Grant Griffin, comp.lang.python


周五,2003年9月26日17:28:02 -0400,Terry Reedy < tj ***** @ udel.edu>

写道:
On Fri, 26 Sep 2003 17:28:02 -0400, "Terry Reedy" <tj*****@udel.edu>
wrote:

" Roberto A. F. De Almeida" < RO ***** @ dealmeida.net>在消息中写道
新闻:10 ************************** @ posting.google。 com ...

"Roberto A. F. De Almeida" <ro*****@dealmeida.net> wrote in message
news:10**************************@posting.google. com...
我发现当使用负索引时,传递给
__getitem__的切片对象取决于切片的数量。一个例子来澄清:

类a:
def __getitem __(自我,索引):
返回索引
I found that when using negative indices, the slice object passed to
__getitem__ depends on the number of slices. An example to clarify:

class a:
def __getitem__(self, index):
return index
>> > b = a()
>>>打印b [: - 1]回溯(最近一次调用最后一次):
文件"< stdin>",第1行,在?
AttributeError:实例没有属性''__len__''

但如果我通过了多个切片:
>>> b = a()
>>> print b[:-1] Traceback (most recent call last):
File "<stdin>", line 1, in ?
AttributeError: a instance has no attribute ''__len__''

But if I pass a "multiple" slice:
>>> print b [: - 1,: - 1]
>>> print b[:-1,:-1]


(切片(无,-1,无),切片(无,-1,无))


(slice(None, -1, None), slice(None, -1, None))




A square-bracketed subscript (index/key) is a *single* object, in this
case a tuple. The contents of the tuple are irrelevant (for this
code). Any tuple will be echoed:




我没有看到任何迹象表明他不知道这一点。


我希望,假设元组包含切片对象

由多切片表示法构造,那么将对切片执行相同的

转换插入

创建单个切片时应用的元组。


即,是使用单个还是多个符号,以及是否

切片对象是否放置在元组中,它们是从元组符号构造的

- 从符号到切片的转换

对象应该是一致地完成。


实际上,在单一案例中完成的翻译让我担心 - 我刚才写的一个

扩展模块如果是这样的话应该不会工作

case(因为切片基于关联键,而不是下标,所以

负下标技巧不适用。


我最好做一些测试,我想: - (

你可能想要的是b [: - 1] [: - 1]等。每个索引必须单独括号以访问列表列表中的项目(等)。



I didn''t see any sign that he wasn''t aware of that.

I would have expected, given that the tuple contains slice objects
constructed from the multiple-slice notation, that the same
translations would be performed on the slices that are inserted into
the tuple that are applied when the single slice is created.

That is, whether the single or multiple notation is used, and whether
the slice objects are placed in a tuple or not, they are constructed
from the tuple notation - the translation from notation to slice
object should be done consistently.

Actually, the translation done in the single case worries me - an
extension module I wrote a while ago should not work if this is the
case (as the slice is based on associative keys, not subscripts, so
the negative subscript trick doesn''t apply).

I''d better do some testing, I suppose :-(
What you probably want is b[:-1][:-1], etc. Each index must be
separately bracketed to access items in list of lists (etc).




也许吧。也许不吧。我假设他做的事情类似于数字中发生的
- 他们做了一些相当有趣的事情。

切片多维容器的东西。当然我是

不是数字用户,所以我可能会误解事情。

-

Steve Horne


steve at ninereeds dot fsnet dot co dot uk



Maybe. Maybe not. I assumed he was doing something similar to what
happens in numeric - they do some rather interesting
slicing-of-multidimensional-container things over there. Of course I''m
not a numeric user, so I may be misunderstanding things.
--
Steve Horne

steve at ninereeds dot fsnet dot co dot uk


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

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