切片操作可能有所改进。 [英] Possible improvement to slice opperations.

查看:76
本文介绍了切片操作可能有所改进。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在考虑了几个替代方案并尝试了一些想法后,Bengt Richter发布了一个修改后的列表对象,(谢谢),我想我已经

找到了一种切片操作的方法(特别是远端索引)

对称且更一致。


所以要找出是否这确实是一种可能性,在这一点上获得很少的意见会很高兴。爆炸......或者希望告诉我

你喜欢它。 ;-)


(任何使其更好的建议或贡献都将受到赞赏。)


干杯,

Ron Adam


"""

改善SLIING

=========== =====


在我看来,切片是Python最好的功能之一,但是当你尝试使用负索引时,它就是
或负步骤增量

它可能很棘手并导致意想不到的结果。


这个主题在comp.lang.python上经常出现,经常出现

次,回复包括:


*初学者应该避免负片扩展切片。


*切片带负片步骤增量适用于高级

python程序员。


*如果你以不同的方式看待它,它并没有破坏。


*你应该采用不同的方式。


所有这些以及与它们相似的各种反应都不能令人满意

my意见并且它是一个很好的指标,它不是那么好。

索引和切片操作在Python中很常见而且我不会
我们应该阻止人们学习和使用它们。

兼容性

-------------

因为以下建议的更改将破坏当前代码,

它可能无法在Python 3000之前实现。


+直接索引,包括正面和负面价值

返回与现在相同的商品。


+所有正面和/或空面默认的扩展切片

值仍然存在没有。


- 带负值的扩展切片返回值

的物品数量比目前少。


- 带负值的切片返回完全不同

结果。

REVERSE ORDER STEPPING

------------- ---------

当使用否定步骤时,a切片操作

执行以下操作。 (或等价物)


1.反转清单

2.使用开始和停止切断反转序列

3。使用步骤的绝对值迭代向前。


*此顺序导致反向选择,我认为应该是

被视为错误。


按以下方式更改订单会产生更多可预测的模式,这种模式更容易理解和使用。


1.使用开始和结束切割序列。

2反转结果的顺序。

3.使用步长的绝对值向前迭代。

CURRENT INDEXING

----------------


给定范围[a,b,c ]:


正数索引


| a | b | c |

+ --- + --- + --- +

0 1 2 3


当前负面索引。


| a | b | c |

+ --- + --- + --- +

-3 -2 -1 -0

单身时index使用该项为

指数的正数和

负指数'的回报。


使用切片,返回start和

停止索引'之间的项目。


以数字形式访问列表末尾的范围当使用负指数时,
变得不方便

因为-0位置无法用数字指定

带负值。

基于负面的指数

----------------------------

根据负面指数的基础,选择

负面指数左边的单个项目将启用

寻址结束数字列表。


基于负指数'。


| a | b | c |

+ --- + --- + --- +

-4 -3 -2 -1


然后:


a [-1] - >索引左侧的c#项目,与现在的结果相同。


a [-3:-2] - > b#index之间的项目


a [-1:-1] = [d]#在最后插入项目。


使用''〜''负面指数'

--------------------------- ------------


''''是二进制非符号,当使用

和整数时返回两个人的赞美。这个

很适合从列表末尾索引

因为方便〜0 == -1。


这会创建积极的

索引和''〜''之间的数字对称性列表的结尾索引。


a [0] - >列表中的第一项。

a [〜0] - >列表中的最后一项。


a [0:~0] - >整个清单。


a [1:~1] - >中心,两端都有一个位置。


*注意:使用''〜''就像这里描述的那样代替单个负面

index''在当前版本的Python中。对于扩展切片,它不像描述那样工作




"""


#测试列表类。

"""

列表类测试''〜''列表索引结束。


*此类在返回

a值之前修改切片。最后的实现可以通过

直接修改切片对象或序列的基础

C代码来实现。


"" ;"


class nxlist(object):


def __init __(self,value):

self .value = value


def normslc(self,slc):

start,stop,step = slc.start,slc.stop,slc.step

如果是type(start)== int并且start< 0:

start = len(self.value)+ start + 1

if type (停止)== int并停止< 0:

stop = len(self.value)+ stop + 1

返回切片(开始,停止,步骤)


def __getitem __(self,i):

tp = i .__ class__

if tp == int:

if i> = 0:

返回self.value [i]

else:

返回self.value [len(self) .value)+ i]

如果tp == slice:

slc = self.normslc(i)

value = self.value [ slc.start:slc.stop]

如果类型(i.step)== int和i.step< 0:

v alue.reverse()

slc = slice(无,无,-i.step)

else:

slc = slice(无,无,i.step)

返回值[slc]


#def __setitem __(self,i,v):

#Not已经说完了。


def __repr __(self):return''nxlist(%r)''%self.value

a = nxlist(range( 10))

打印一个


testdata = [

(''a [0]''),
(''a [1]''),

(''[[0]''),

(''a [〜 1]''),

('a [:]''),

(''[[~~ 1]''),

(''a [~1:]''),

(''a [::]''),

(''a [ 0:~0]''),

(''[1:~1]''),

(''a [~1:1]' '),

(''a [:: - 2]''),

(''[[3]''),

(''a [3:]''),

(''a [~3:]''),

(''a [: ~3]''),

(''[:3:-1]''),

(''a [3 :: - 1]' '),

(''a [~3 :: - 1]''),

(''[:~3:-1]''),

(''a [:3: - 2]''),

(''[3 :: - 2]''),

(''a [~3 :: - 2]' '),

(''[:~3:-2]''),

]


代表n ,s in enumerate(testdata):

print''%r。 %s =%r''%(n,s,eval(s))

"""

nxlist([0,1,2,3,3 ,4,5,6,7,8,9])

0. a [0] = 0

1. a [1] = 1

2. a [~0] = 9

3. a [~1] = 8

4. a [:] = [0,1, 2,3,4,5,6,7,8,9]

5. a [:~1] = [0,1,2,3,4,5,6,7, 8]

6. a [~1:] = [9]

7. a [::] = [0,1,2,3,4,5 ,6,7,8,9]

8. a [0:~0] = [0,1,2,3,4,5,6,7,8,9]

9. a [1:~1] = [1,2,3,4,5,6,7,8]

10. a [~1:1] = []

11. a [:: - 2] = [9,7,5,3,1]

12. a [:3] = [0 ,1,2]

13. a [3:] = [3,4,5,6,7,8,9]

14. a [~3 :] = [7,8,9]

15. a [:~3] = [0,1,2,3,4,5,6]

16. a [:3:-1] = [2,1,0]

17. a [3 :: - 1] = [9,8,7,6,5,4, 3]

18. a [~3 :: - 1] = [9,8,7]

19. a [:~3:-1] = [ 6,5,4,3,2,1,0]

20. a [:3:-2] = [2,0]

21. a [ 3 :: - 2] = [9,7,5,3]

22. a [~3 :: - 2] = [9,7]

23. a [:~3:-2] = [6,4,2,0]

"""

r = range( 10)

a = nxlist(r)


打印r [〜0],[〜0]#ok

打印r [~3:],一个[~3:]#一个关闭

打印r [~3 :: - 1],一个[~3 :: - 1]#另一边
打印r [~3 :: - 2],[~3 :: - 2]#另一面

"""

比较负指数和''〜''
索引相同的值。


当前,建议


9 9

[6,7,8,9] [7,8,9]

[6,5,4,3,2,1,0] [ 9,8,7]

[6,4,2,0] [9,7]

"""


After considering several alternatives and trying out a few ideas with a
modified list object Bengt Richter posted, (Thank You), I think I''ve
found a way to make slice operation (especially far end indexing)
symmetrical and more consistent.

So to find out if this is indeed a possibility, it would be nice to get
a few opinions at this point. So blast away... or hopefully tell me
what you like about it instead. ;-)

(Any suggestions or contributions to make it better would be appreciated.)

Cheers,
Ron Adam

"""
IMPROVED SLICING
================

Slicing is one of the best features of Python in my opinion, but
when you try to use negative index''s and or negative step increments
it can be tricky and lead to unexpected results.

This topic has come up fairly often on comp.lang.python, and often
times, the responses include:

* Beginners should avoid negative extended slices.

* Slices with negative step increments are for advanced
python programmers.

* It''s not broke if you look at it in a different way.

* You should do it a different way.

All of these and various responses similar to them are unsatisfactory in
my opinion and it''s a good indicator it''s not as good as it could be.
Indexing and slice operations are vary common in Python and I don''t
think we should be discouraging people from learning and using them.
COMPATIBILITY
-------------
Because the following suggested changes will break current code,
it probably can not be implemented prior to Python 3000.

+ Direct indexing with both positive and negative values
returns the same items as they do now.

+ Extended slices with all positive and or empty default
values remain unchanged.

- Extended slices with negative values return values that
have less items than currently.

- Slices with negative step values return entirely different
results.
REVERSE ORDER STEPPING
----------------------
When negative steps are used, a slice operation
does the following. (or the equivalent)

1. reverse the list
2. cut the reversed sequence using start and stop
3. iterate forward using the absolute value of step.

* This order results in an inverse selection and I believe should be
considered a bug.

Changing the order in the following way results in a much
more predictable pattern which is both easier to understand and use.

1. cut sequence using start and stop.
2 reverse the order of the results.
3. iterate forward using the absolute value of step.
CURRENT INDEXING
----------------

Given a range [a,b,c]:

Positive indexing

| a | b | c |
+---+---+---+
0 1 2 3

Current negative indexing.

| a | b | c |
+---+---+---+
-3 -2 -1 -0
When a single index is used the item to the
right of the index for both positive and
negative index''s is returned.

With slices, the items between start, and
stop index''s are returned.

Accessing a range at the end of a list numerically
becomes inconvenient when negative index''s are used
as the ''-0''th position can not be specified numerically
with negative values.
ONES BASED NEGATIVE INDEXING
----------------------------
Making negative index''s Ones based, and selecting
individual item to the left of negative index''s would enable
addressing the end of the list numerically.

Ones based negative index''s.

| a | b | c |
+---+---+---+
-4 -3 -2 -1

Then:

a[-1] -> c # item left of index, same result as now.

a[-3:-2] -> b # item between index''s

a[-1:-1] = [d] # insert item at the end.

USE OF ''~'' IN PLACE OF NEGATIVE INDEX''S
---------------------------------------

The ''~'' is the binary not symbol which when used
with integers returns the two''s compliment. This
works nice with indexing from the end of a list
because convieniently ~0 == -1.

This creates a numerical symmetry between positive
indexing and ''~'' "End of list" indexing.

a[0] -> first item in the list.
a[~0] -> last item in the list.

a[0:~0] -> whole list.

a[1:~1] -> center, one position from both ends.

* Note: using ''~'' works as described here in place of single negative
index''s in current versions of Python. It does not work as described
here for extended slices.

"""

# TEST LIST CLASS.
"""
A list class to Test ''~'' end of list indexing.

* This class modifies the slice before returning
a value. The final implementation may do this by
modifying slice objects directly or the underlying
C code of sequences.

"""

class nxlist(object):

def __init__(self, value):
self.value = value

def normslc(self, slc):
start,stop,step = slc.start, slc.stop, slc.step
if type(start) == int and start<0:
start = len(self.value)+start+1
if type(stop) == int and stop<0:
stop = len(self.value)+stop+1
return slice(start,stop,step)

def __getitem__(self, i):
tp = i.__class__
if tp == int:
if i>=0:
return self.value[i]
else:
return self.value[ len(self.value)+i ]
if tp == slice:
slc = self.normslc(i)
value = self.value[slc.start:slc.stop]
if type(i.step) == int and i.step<0:
value.reverse()
slc = slice(None,None,-i.step)
else:
slc = slice(None,None,i.step)
return value[slc]

#def __setitem__(self, i, v):
#Not emplimented yet.

def __repr__(self): return ''nxlist(%r)''%self.value
a = nxlist(range(10))
print a

testdata = [
(''a[0]''),
(''a[1]''),
(''a[~0]''),
(''a[~1]''),
(''a[:]''),
(''a[:~1]''),
(''a[~1:]''),
(''a[::]''),
(''a[0:~0]''),
(''a[1:~1]''),
(''a[~1:1]''),
(''a[::-2]''),
(''a[:3]''),
(''a[3:]''),
(''a[~3:]''),
(''a[:~3]''),
(''a[:3:-1]''),
(''a[3::-1]''),
(''a[~3::-1]''),
(''a[:~3:-1]''),
(''a[:3:-2]''),
(''a[3::-2]''),
(''a[~3::-2]''),
(''a[:~3:-2]''),
]

for n, s in enumerate(testdata):
print ''%r. %s = %r'' % (n,s,eval(s))
"""
nxlist([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
0. a[0] = 0
1. a[1] = 1
2. a[~0] = 9
3. a[~1] = 8
4. a[:] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
5. a[:~1] = [0, 1, 2, 3, 4, 5, 6, 7, 8]
6. a[~1:] = [9]
7. a[::] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
8. a[0:~0] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
9. a[1:~1] = [1, 2, 3, 4, 5, 6, 7, 8]
10. a[~1:1] = []
11. a[::-2] = [9, 7, 5, 3, 1]
12. a[:3] = [0, 1, 2]
13. a[3:] = [3, 4, 5, 6, 7, 8, 9]
14. a[~3:] = [7, 8, 9]
15. a[:~3] = [0, 1, 2, 3, 4, 5, 6]
16. a[:3:-1] = [2, 1, 0]
17. a[3::-1] = [9, 8, 7, 6, 5, 4, 3]
18. a[~3::-1] = [9, 8, 7]
19. a[:~3:-1] = [6, 5, 4, 3, 2, 1, 0]
20. a[:3:-2] = [2, 0]
21. a[3::-2] = [9, 7, 5, 3]
22. a[~3::-2] = [9, 7]
23. a[:~3:-2] = [6, 4, 2, 0]
"""
r = range(10)
a = nxlist(r)

print r[~0],a[~0] # ok
print r[~3:],a[~3:] # one off
print r[~3::-1],a[~3::-1] # other side
print r[~3::-2],a[~3::-2] # other side
"""
Comparisons of negative indexing and ''~''
indexing with same values.

current, proposed

9 9
[6, 7, 8, 9] [7, 8, 9]
[6, 5, 4, 3, 2, 1, 0] [9, 8, 7]
[6, 4, 2, 0] [9, 7]
"""

推荐答案



" Ron Adam" < rr*@ronadam.com>在消息中写道

新闻:FR ****************** @ tornado.tampabay.rr.com。 ..

"Ron Adam" <rr*@ronadam.com> wrote in message
news:FR******************@tornado.tampabay.rr.com. ..
在我看来,切片是Python的最佳功能之一,但是当你尝试使用负索引和/或负步增量时它可以是
它可以是这个主题在comp.lang.python上经常出现,而且往往,响应包括:
*初学者应该避免否定扩展切片。
*具有负步长增量的切片适用于高级程序员。
*如果以不同的方式查看它,它就不会破坏。
*你应该采取不同的方式。
Slicing is one of the best features of Python in my opinion, but
when you try to use negative index''s and or negative step increments
it can be tricky and lead to unexpected results.

This topic has come up fairly often on comp.lang.python, and often times,
the responses include:
* Beginners should avoid negative extended slices.
* Slices with negative step increments are for advanced
python programmers.
* It''s not broke if you look at it in a different way.
* You should do it a different way.




您省略了我之前给出的特定于切片的响应,并在此重复了

更多细节来自引用Python 2.3中的新功能。
http://www.python.org/doc/2.3/whatsn...on-slices.html

"

15分机截断片段

自从Python 1.4以来,切片语法支持可选的第三个

步''或''stride''''参数。例如,这些都是合法的Python

语法:L [1:10:2],L [: - 1:1],L [:: - 1]。这是在Python的开发人员的

请求中添加到Python中的,该请求广泛使用第三个

参数。但是,Python的内置列表,元组和字符串

序列类型从未支持此功能,如果您尝试了

则会引发TypeError。

"

再一次,扩展切片可能是设计用的,当然是专为

设计的。数值Python和7年的使用至少主要是由数字

Python。它们不是为像您这样的其他用户设计的。答案

你总结和干扰几乎都来自这段历史。


所以,我很确定核心的变化必须向上

与当前使用情况兼容。另一方面,你的nxlist子类

列表似乎现在运行得很好.2.2 +这种类型的b / b $ b的能力是什么类型的子类化是为。


或者可以编写类似于.normslc

方法的扩展切片函数。我甚至有一天会用这样的东西。


Terry J. Reedy



You omitted the slice-specific response I gave before and repeat here with
more detail by quoting from What''s New in Python 2.3.
http://www.python.org/doc/2.3/whatsn...on-slices.html
"
15 Extended Slices
Ever since Python 1.4, the slicing syntax has supported an optional third
``step'''' or ``stride'''' 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.
"
Again, extended slices were probably designed by and certainly designed for
Numerical Python and for 7 years were used at least mainly by Numerical
Python. They were not designed for other users like you. The responses
you summarized and distain pretty much all derive from this history.

So, I am pretty sure that changes to the core would have to be upwards
compatible with current usage. On the other hand, your nxlist subclass of
list seems to work pretty well now.The 2.2+ ability to do this sort of
thing is what type subclassing was made for.

Or one could just write an extended slice function similar to your .normslc
method. I might even use such a thing one day.

Terry J. Reedy


>在考虑了几个替代方案并尝试使用
> After considering several alternatives and trying out a few ideas with a
修改后的列表对象Bengt Richter发布的一些想法后,(谢谢),我想我已经找到了一种切片操作的方法(特别是远端索引)
对称且更一致。
modified list object Bengt Richter posted, (Thank You), I think I''ve
found a way to make slice operation (especially far end indexing)
symmetrical and more consistent.




我不知道它使它更加一致。我可以被说服,

但它必须是现实生活中的例子,计算切片

指数和步幅。我一直这样做,并且发现当前的

规则简单而且非常一致。偶尔,我可能希望

的东西有点不同,但总有一种解决方法。我需要看到一些真实的代码示例,其范围足以看到

这个提案的解决方法比

更少。目前的实施。


FWIW,对于指数

可能为负且你想要零或更大来表示''的情况有一个合理的解决方法结束

list''。如果是x,是索引变量,您可以使用表达式(x <0

和x或None)作为切片中的索引值。如果你发现自己经常这样做,你可以为它写一个小函数 - def

EndIndex(x):返回x< 0和x或None。


但是在实际代码中,我担心你可能需要一个类似的辅助函数来解决你的变化类似的问题。我只是不知道那些是什么

没有多想。


问候,

Pat



I don''t know that it makes it more consistent. I could be persuaded,
but it would have to be by real-life examples with calculated slice
indices and stride. I do this thing all the time, and find the current
rules simple and very consistent. Occasionally, I might wish that
things were a little different, but there is always a workaround. I
would have to see some real code examples, of sufficient scope to see
that there are fewer workarounds with this proposal than with the
current implementation.

FWIW, there is a reasonable workaround for the case where the indices
might be negative and you would like zero or greater to mean ''end of
list''. If "x" is the index variable, you can use the expression (x<0
and x or None) for the index value in the slice. If you find yourself
doing this often, you can write a little function for it -- def
EndIndex(x): return x<0 and x or None.

But in real code, I fear you might need a similar helper function for
similar issues with your change. I just don''t know what those are
without more thought.

Regards,
Pat


Terry Reedy写道:
Terry Reedy wrote:
" Ron Adam" < rr*@ronadam.com>在消息中写道
新闻:FR ****************** @ tornado.tampabay.rr.com。 ..
"Ron Adam" <rr*@ronadam.com> wrote in message
news:FR******************@tornado.tampabay.rr.com. ..
在我看来,切片是Python的最佳功能之一,但是当你尝试使用负索引和/或负步增量时,
它可能很棘手并导致意想不到的结果。

这个主题在comp.lang.python上经常出现,而且往往,
响应包括:
*初学者应避免使用负片扩展切片。
*具有负步长增量的切片适用于高级程序员。
*如果以不同的方式查看它,它就不会破坏。 /> *你应该采用不同的方式。

你省略了我之前给出的特定于切片的响应,并在这里重复了更多细节,引用了Python 2.3中的新功能。
http://www.python .org / doc / 2.3 / whatsn ... on-slices.html
"
15个扩展切片
自Python 1以来.4,切片语法支持可选的第三个步'或''stride''''参数。例如,这些都是合法的Python语法:L [1:10:2],L [: - 1:1],L [:: - 1]。这是在Numerical Python的开发人员的要求下添加到Python中的,它广泛使用了第三个
参数。但是,Python的内置列表,元组和字符串
序列类型从未支持此功能,如果您尝试了它,则会引发TypeError。
"
Slicing is one of the best features of Python in my opinion, but
when you try to use negative index''s and or negative step increments
it can be tricky and lead to unexpected results.

This topic has come up fairly often on comp.lang.python, and often times,
the responses include:
* Beginners should avoid negative extended slices.
* Slices with negative step increments are for advanced
python programmers.
* It''s not broke if you look at it in a different way.
* You should do it a different way.

You omitted the slice-specific response I gave before and repeat here with
more detail by quoting from What''s New in Python 2.3.
http://www.python.org/doc/2.3/whatsn...on-slices.html
"
15 Extended Slices
Ever since Python 1.4, the slicing syntax has supported an optional third
``step'''' or ``stride'''' 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.
"




我没有遗漏任何东西,我之前给出了使用各种

形式的切片的例子,目的是展示其中一些形式是如何/>
不对称且难以理解。然后你更准确地解释了

如何对这些切片进行索引。


对这些案例进行并排比较是非常有用的。 >
类似于这种新方法。那么也许改变

的好处我建议会更清楚。


我查看了Numerical Python中的文档。从我所知道的,

它使用L [:: - 1这个形式相当多,它现在仍然可以*完全*,现在它是* b $ b。没有部分否定步骤

操作的例子,所以我无法确定它们是否/如何使用。我会安装

它并试验它并查看它是否/如何与Numerical Python一起使用。


Python 3000发布后数值Python将需要待更新

即使没有这些变化。但是最好咨询一下这个数值Python开发人员。


同样,扩展切片可能是设计用于


像我这样的其他用户?我是一个什么样的用户?


保持一些事情不应该被某些事情做的确是个好主意

对其他人好吗?


我认为你的意思是说只有(或主要)与Numerical Python一起使用的延长切片具有负步幅,

。但是,我不同意这种观点。我认为它们有更广泛的用途。


因此,我非常确定核心的更改必须向上兼容当前使用情况。 ...


这就是为什么我说这些变化可能在Python之前无法实现
3000.它是向上(向后?)兼容的大多数使用

的情况,所有直接索引操作,以及所有正扩展切片。

在Python的库中,只有负扩展切片需要

调整。据我所知,没有部分负面进展

需要调整的案例。我希望有一些,因为

然后我可以展示这将如何改善这些情况。


现在即使这个建议可能是合理的,仅此一项并不是理由

足以将它包含在Python 3000中。它需要明确的用例

用户的好处和支持以及Guido的支持。


此时,我只是想澄清问题并开发工作

例子。

...另一方面你的nxlist子列表似乎现在工作得很好。 2.2+做这种事情的能力是
类型的子类化。


是的,它运作良好,(到目前为止):-)这是我的动机

发布它。让它变得更好。


或者可以编写类似于.normslc
方法的扩展切片函数。我甚至可能有一天会使用这样的东西。


不幸的是,不能直接对切片对象进行子类化。并且切片索引中的'':''

并不是一个真正的运算符,它有自己的方法。因此,改变现有切片行为需要更改核心。但是

现在我会写一个函数和一个完整的列表子类并在这里发布




干杯,

Ron

Terry J. Reedy



I didn''t omit anything, I previously gave examples of using various
forms of slices with the intent of showing how some of those forms are
not symmetrical and difficult to understand. And then you explained
more precisely how those slices were indexed.

It would be useful to do a side by side comparison of those cases and
similar ones to this new method. Then maybe the benefits of making the
changes I''m suggesting will be clearer.

I looked at the documents in Numerical Python. From what I could tell,
it uses the form L[::-1] quite a bit, which will still work *exactly* as
it does now. There were no examples of partial negative step
operations, so I couldn''t determine if/how they are used. I''ll install
it and experiment with it and see if/how it works with Numerical Python.

After Python 3000 is released Numerical Python will need to be updated
even without these changes. But it is a good idea to consult with the
Numerical Python developers on this.

Again, extended slices were probably designed by and certainly designed for
Numerical Python and for 7 years were used at least mainly by Numerical
Python. They were not designed for other users like you. The responses
you summarized and distain pretty much all derive from this history.
Other users like me? And what kind of user am I?

Is it really a good idea to maintain that some things shouldn''t be done
by some, but is ok for others?

I think you mean to say that extended slices with negative strides,
should only (or primarily) be used with Numerical Python. I however
disagree with that view. I think they have a broader range of uses.

So, I am pretty sure that changes to the core would have to be upwards
compatible with current usage. ...
This is why I said these changes probably couldn''t be made before Python
3000. It is upward (backward?) compatible for the majority of use
cases, all direct indexing operations, and all positive extended slices.
In Python''s library, only negative extended slices will need to be
adjusted. As far as I could tell there are no partial negative strides
cases that will need to be adjusted. I wish there was a few, because
then I could show how this would improve those cases.

Now even though this suggestion may be sound, that alone isn''t reason
enough to include it in Python 3000. It would need clear use case
benefits and support by users as well as support from Guido.

At this point, I''m only trying to clarify the issues and develop working
examples.
... On the other hand, your nxlist subclass of list seems to work
pretty well now. The 2.2+ ability to do this sort of thing is what
type subclassing was made for.
Yes, it does work well, (so far) :-) Which is the motivation for me
posting it. Lets make it better.

Or one could just write an extended slice function similar to your .normslc
method. I might even use such a thing one day.
Unfortunately one can not sub-class slice objects directly. And the '':''
in a slice index isn''t a true operator with it''s own methods. So
changing existing slice behavior will require changing the core. But
for now I will write a function and a complete list sub-class and post
it here.

Cheers,
Ron
Terry J. Reedy



这篇关于切片操作可能有所改进。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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