Scheiben in Python
sequence[ start: top: step ]
- Kompakte Darstellung mit Unterprogrammen
def wrap( x, length ):
if x < 0: x += length
return xdef clip( x, length, is_negative ):
if x > length - is_negative: x = length - is_negative
if x < 0: x = -is_negative
return xdef clipandwrap( x, length, is_negative ):
x = wrap( x, length )
x = clip( x, length, is_negative )
return xdef sl( sequence, start, top, step ):
is_negative = +( step <= 0 )
clipwrap = lambda var: clipandwrap( var, len( sequence ), is_negative )
start = clipwrap( start )
top = clipwrap( top )
i = start
while i > top if is_negative else i < top:
yield sequence[ i ]
i += step- Kompakte Darstellung
def sl( sequence, start, top, step ):
length = len( sequence )
negative = +( step <= 0 )if start < 0: start += length
if start > length - negative: start = length - negative
if start < 0: start = -negativeif top < 0: top += length
if top > length - negative: top = length - negative
if top < 0: top = -negativei = start
while i > top if negative else i < top:
yield sequence[ i ]
i += step- Getrennt nach positiver und negativer Schrittweite
def sl( sequence, start, top, step ):
length = len( sequence )
return( slpos if step > 0 else slneg )( sequence, length, start, top, step )# step is not negative
def slpos( sequence, length, start, top, step ):if start < 0: start += length
if start > length: start = length
if start < 0: start = 0if top < 0: top += length
if top > length: top = length
if top < 0: top = 0i = start
while i < top:
yield sequence[ i ]
i += step# step is negative
def slneg( sequence, length, start, bottom, step ):
step = -stepif start < 0: start += length
if start > length - 1: start = length - 1
if start < 0: start = -1if bottom < 0: bottom += length
if bottom > length - 1: bottom = length - 1
if bottom < 0: bottom = -1i = start
while i > bottom:
yield sequence[ i ]
i -= step- Weiter aufgeteilt
def sl( sequence, start, top, step ):
length = len( sequence )
return( slpos if step > 0 else slneg )( sequence, length, start, top, step )def slpos( sequence, length, start, top, step ):
return( slpospp if start >= 0 and top >= 0 else slposxx )( sequence, length, start, top, step )# case: no value is negative
def slpospp( sequence, length, start, top, step ):if start > length: start = length
if top > length: top = length# case: all values are in range
i = start
while i < top:
yield sequence[ i ]
i += step# case: step is not negative, but start or top might be negative
def slposxx( sequence, length, start, top, step ):
if start < 0: start += length
if start > length: start = length
if start < 0: start = 0
if top < 0: top += length
if top > length: top = length
if top < 0: top = 0
i = start
while i < top:
yield sequence[ i ]
i += step# case: step is negative
def slneg( sequence, length, start, bottom, step ):
if start < 0: start += length
if start > length - 1: start = length - 1
if start < 0: start = -1
if bottom < 0: bottom += length
if bottom > length - 1: bottom = length - 1
if bottom < 0: bottom = -1
i = start
step = -step
while i > bottom:
yield sequence[ i ]
i -= step- Bis zum Ende gehen
>>> 'abc'[ 0: len( 'abc' )]
'abc'
>>> 'abc'[ 0: None ]
'abc'
>>> 'abc'[ 0: int(9E99) ]
'abc'