slices in Python (slices in Python), Lektion, Seite 724120
https://www.purl.org/stefan_ram/pub/slices_python (Permalink) ist die kanonische URI dieser Seite.
Stefan Ram
Python-Kurs

Scheiben in Python 

sequence[ start: top: step ]

Kompakte Darstellung mit Unterprogrammen

def wrap( x, length ):
if x < 0: x += length
return x

def clip( x, length, is_negative ):
if x > length - is_negative: x = length - is_negative
if x < 0: x = -is_negative
return x

def clipandwrap( x, length, is_negative ):
x = wrap( x, length )
x = clip( x, length, is_negative )
return x

def 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 = -negative

if top < 0: top += length
if top > length - negative: top = length - negative
if top < 0: top = -negative

i = 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 = 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

# step is negative
def slneg( sequence, length, start, bottom, step ):
step = -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
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'

 

Seiteninformationen und Impressum   |   Mitteilungsformular  |   "ram@zedat.fu-berlin.de" (ohne die Anführungszeichen) ist die Netzpostadresse von Stefan Ram.   |   Eine Verbindung zur Stefan-Ram-Startseite befindet sich oben auf dieser Seite hinter dem Text "Stefan Ram".)  |   Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram. Schlüsselwörter zu dieser Seite/relevant keywords describing this page: Stefan Ram Berlin slrprd slrprd stefanramberlin spellched stefanram724120 stefan_ram:724120 slices in Python Stefan Ram, Berlin, and, or, near, uni, online, slrprd, slrprdqxx, slrprddoc, slrprd724120, slrprddef724120, PbclevtugFgrsnaEnz Erklärung, Beschreibung, Info, Information, Hinweis,

Der Urheber dieses Textes ist Stefan Ram. Alle Rechte sind vorbehalten. Diese Seite ist eine Veröffentlichung von Stefan Ram.
https://www.purl.org/stefan_ram/pub/slices_python