通常只切片列表/元组的一个元素?

时间:2014-07-31 02:29:59

标签: python linked-list subclassing slice

这个slice(-1,0)从一段实际代码中掉出来,看起来像是一个切片包装,差不多,但它不是。

评论:我怀疑是一个真正的"切片包装"将是另一个挑战。

在"几乎包裹" slice,简单的解决方法是在这种特殊情况下删除上限:

AEIOU[-1:], AEIOU[-1:None], or (more quirkilly) AEIOU[-1:5]

更普遍的尝试:(我正在使用: Python 2.6.6

AEIOU[start%len(AEIOU): stop%len(AEIOU)] # simply struggles. 

但鉴于负面成员函数__getitem__总能正常工作,所以有一种奇怪的(危险的)是一种近乎包裹的"成员__getslice__只是返回太短的东西。

问题:

  1. 是否有一个特定的设计理由,即"近包装"切片很短,而不是(可能)提出IndexError: (list/string/tuple) index out of range" exception

  2. 如何创建列表的子类来管理它?

  3. Python 3.0 的处理方式有何不同?

  4. 演示:

    >>> AEIOU="AEIOU"
    >>> for i in range(-len(),5,1): print "%-16s, %r, %r"%(slice(i,i+1,1),AEIOU[i],AEIOU[i:i+1])
    ... 
    slice(-5, -4, 1), 'A', 'A'
    slice(-4, -3, 1), 'E', 'E'
    slice(-3, -2, 1), 'I', 'I'
    slice(-2, -1, 1), 'O', 'O'
    slice(-1, 0, 1) , 'U', '' # missing U...
    slice(0, 1, 1)  , 'A', 'A'
    slice(1, 2, 1)  , 'E', 'E'
    slice(2, 3, 1)  , 'I', 'I'
    slice(3, 4, 1)  , 'O', 'O'
    slice(4, 5, 1)  , 'U', 'U'
    

    我注意到了一般的"解决方法"是:

    >>> i=-1; AEIOU[i:i%len(AEIOU)+1]
    'U' # found U
    

    勘误表,切片示例:

    >>> s=3
    >>> for i in range(-len(AEIOU),5,1): print "%-16s, %r, %r, %r"%(slice(i,i%len(AEIOU)+s,1),AEIOU[i],AEIOU[i:i+s], AEIOU[i:i%len(AEIOU)+s])
    ... 
    slice(-5, 3, 1) , 'A', 'AEI', 'AEI'
    slice(-4, 4, 1) , 'E', 'EIO', 'EIO'
    slice(-3, 5, 1) , 'I', '', 'IOU'
    slice(-2, 6, 1) , 'O', '', 'OU'
    slice(-1, 7, 1) , 'U', '', 'U'
    slice(0, 3, 1)  , 'A', 'AEI', 'AEI'
    slice(1, 4, 1)  , 'E', 'EIO', 'EIO'
    slice(2, 5, 1)  , 'I', 'IOU', 'IOU'
    slice(3, 6, 1)  , 'O', 'OU', 'OU'
    slice(4, 7, 1)  , 'U', 'U', 'U'
    

2 个答案:

答案 0 :(得分:1)

负数指数从列表末尾开始倒数,但如果它们足够大,它们可以超过开头。当他们超调时,他们只需固定到列表中的第一项。

items = range(10)
items[-10:-9]
# 0
items[-20:-9]
# 0

正向指数从列表的开头开始计算。如果它们超调,它们也会在列表末尾固定:

items[9:10]
#[9]

items[9:100]
#[9]

与常规的 getitem 访问不同,列表边界之外的索引只是限制在最后。通常这会返回一个空切片:

items[99:100]
#[]

items[-99:-88]
#[]

你的例子中出现的特殊情况是因为'aeiou'[-1:0]'aeiou'[4:0]是一回事 - 而且因为这是一个负长度但是正步幅的切片,所以它是空的。如果你输入'aeiou'[4:0:-1],你将会回来'uoie'

由于固定行为,无法保证返回的切片将包含与说明符之间距离相同的项目数。切片是一个很好的习惯用于获取没有事先检查的项目,这与我认为你要求的相反:

stuff = ['a','b','c']
d = stuff[11:12]
# []

stuff[11]
#IndexError: list index out of range

如果您确实需要知道您的查询超出范围,则必须预先检查索引:

def strict_slice(the_list, begin, end):
    listlen = len(the_list)
    if begin > listlen or begin + listlen < 0:
        raise IndexError, 'start index out of bounds'
    if end + listlen < 0 or end > listlen:
        raise IndexError, 'end index out of bounds'
    return the_list[begin:end]


test = 'abcdefg'
strict_slice(test, 1, 3)
strict_slice(test, -10, 3)
# IndexError: start index out of bounds
strict_slice(test, 1, 20)
#IndexError: end index out of bounds

虽然您可以继承list来实现这一点,但我似乎更容易使用此配方或只是检查空切片。

答案 1 :(得分:0)

有时元组&amp;列表包装问题(通过&#34; [ - 1:0]和#34;边界)可以用&#34; MixIn&#34;修补。类。

e.g。的代码:

#!/usr/bin/env python

def wrapslice(*sl):
  if len(sl)==1: sl=sl[0]
  if isinstance(sl, int): return sl
  else:
    if not isinstance(sl,slice): sl=slice(*sl) 
    if None in (sl.start,sl.stop): return sl
    elif sl.start>=0 or sl.stop<0: return sl
    else: return slice(sl.start, None, sl.step)

class WrapMixin(object):
# Not sure how to use a decorator on a MixIn, so I use some "boilerplate" code...
  def __getitem__(self,sl):
    return super(WrapMixin,self).__getitem__(wrapslice(sl))
  def __setitem__(self,sl, value):
    return super(WrapMixin,self).__setitem__(wrapslice(sl), value)
  def __delitem__(self,sl):
    return super(WrapMixin,self).__delitem__(wrapslice(sl))

# Note: for __getslice__ [-1:2] converts to [-1+len:2]... 
#       i.e. add len to any -ve numbers! Ouch...
  def __getslice__(self,start,stop):
    if start>stop: stop+=len(self)
    sl=wrapslice(start,stop)
    return super(WrapMixin,self).__getslice__(sl.start, sl.stop)
  def __setslice__(self,start,stop, value):
    if start>stop: stop+=len(self)
    sl=wrapslice(start,stop)
    return super(WrapMixin,self).__setslice__(sl.start, sl.stop, value)
  def __delslice__(self,start,stop):
    if start>stop: stop+=len(self)
    sl=wrapslice(start,stop)
    return super(WrapMixin,self).__delslice__(sl.start, sl.stop)
  # Todo: def append/insert/append/iadd/radd ...

#class WrapString(WrapMixin, string): pass # FAIL
class WrapTuple(WrapMixin, tuple): pass # GOOD
class WrapList(WrapMixin, list): pass # GOOD

if __name__=="__main__": # test case
  aeiou=list("aeiou")
  AEIOU=WrapList("AEIOU")
  len_slice=3
  for i in range(-5,5,1): 
    vanilla=slice(i,i+len_slice)
    fixed=wrapslice(vanilla)
    print AEIOU[i]+"...","Vanilla:",vanilla,"vs Patched:",fixed
    print "  get_slice VANILLA: %15r -> PATCHED: %15r"%(aeiou[vanilla.start:vanilla.stop], AEIOU[vanilla.start:vanilla.stop])
    print "  get_item  VANILLA: %15r -> PATCHED: %15r"%(aeiou[vanilla], AEIOU[vanilla])

<强>输出:

A... Vanilla: slice(-5, -2, None) vs Patched: slice(-5, -2, None)
  get_slice VANILLA: ['a', 'e', 'i'] -> PATCHED: ['A', 'E', 'I']
  get_item  VANILLA: ['a', 'e', 'i'] -> PATCHED: ['A', 'E', 'I']
E... Vanilla: slice(-4, -1, None) vs Patched: slice(-4, -1, None)
  get_slice VANILLA: ['e', 'i', 'o'] -> PATCHED: ['E', 'I', 'O']
  get_item  VANILLA: ['e', 'i', 'o'] -> PATCHED: ['E', 'I', 'O']
I... Vanilla: slice(-3, 0, None) vs Patched: slice(-3, None, None)
  get_slice VANILLA:              [] -> PATCHED: ['I', 'O', 'U']
  get_item  VANILLA:              [] -> PATCHED: ['I', 'O', 'U']
O... Vanilla: slice(-2, 1, None) vs Patched: slice(-2, None, None)
  get_slice VANILLA:              [] -> PATCHED:      ['O', 'U']
  get_item  VANILLA:              [] -> PATCHED:      ['O', 'U']
U... Vanilla: slice(-1, 2, None) vs Patched: slice(-1, None, None)
  get_slice VANILLA:              [] -> PATCHED:           ['U'] <= Found U!
  get_item  VANILLA:              [] -> PATCHED:           ['U']
A... Vanilla: slice(0, 3, None) vs Patched: slice(0, 3, None)
  get_slice VANILLA: ['a', 'e', 'i'] -> PATCHED: ['A', 'E', 'I']
  get_item  VANILLA: ['a', 'e', 'i'] -> PATCHED: ['A', 'E', 'I']
E... Vanilla: slice(1, 4, None) vs Patched: slice(1, 4, None)
  get_slice VANILLA: ['e', 'i', 'o'] -> PATCHED: ['E', 'I', 'O']
  get_item  VANILLA: ['e', 'i', 'o'] -> PATCHED: ['E', 'I', 'O']
I... Vanilla: slice(2, 5, None) vs Patched: slice(2, 5, None)
  get_slice VANILLA: ['i', 'o', 'u'] -> PATCHED: ['I', 'O', 'U']
  get_item  VANILLA: ['i', 'o', 'u'] -> PATCHED: ['I', 'O', 'U']
O... Vanilla: slice(3, 6, None) vs Patched: slice(3, 6, None)
  get_slice VANILLA:      ['o', 'u'] -> PATCHED:      ['O', 'U']
  get_item  VANILLA:      ['o', 'u'] -> PATCHED:      ['O', 'U']
U... Vanilla: slice(4, 7, None) vs Patched: slice(4, 7, None)
  get_slice VANILLA:           ['u'] -> PATCHED:           ['U']
  get_item  VANILLA:           ['u'] -> PATCHED:           ['U']

注意:字符串类型不能被子类化,因此无法修补 另外:致电会员__getslice__&amp;带有负面开头的__setslice__:stop args很棘手,因为这些参数会被&#39;摆弄。由翻译...因此额外的补丁代码......