[Python-de] 1,2,3,5,7,8,9 -> "1-3,5,7-9"
Alexander 'boesi' Bösecke
boesi.josi at gmx.net
Fre Jun 18 11:25:31 CEST 2004
- Vorherige Nachricht: [Python-de] 1,2,3,5,7,8,9 -> "1-3,5,7-9"
- Nächste Nachricht: [Python-de] 1,2,3,5,7,8,9 -> "1-3,5,7-9"
- Nachrichten sortiert nach:
[ Datum ]
[ Thema ]
[ Betreff (Subject) ]
[ Autor ]
Hi
Also ich antworte mir mal selbst
Hab die Zustandsmaschine noch ein wenig optimiert und ne Funktion draus
gemacht:
---snip---
def formatListe(liste, opt=2):
"""
[1,2,3,5,7,8,9,11] soll als "1-3,5,7-9,11" ausgegeben werden
Parameter: liste - zu formatierende Liste
opt - 2: [2,4,5,7] wird zu "2,4,5,7"
3: [2,4,5,7] wird zu "2,4-5,7"
"""
liste.sort()
zustand = 1
c = 0
ret = '%d' % liste[c]
for c in range(1,len(liste)):
if zustand == 1:
if liste[c] != liste[c-1]+1:
ret += ',%d' % liste[c]
else:
zustand = opt
elif zustand == 2:
if liste[c] != liste[c-1]+1:
zustand = 1
ret += ',%d,%d' % (liste[c-1], liste[c])
else:
zustand = 3
elif zustand == 3:
if liste[c] != liste[c-1]+1:
zustand = 1
ret += '-%d,%d' % (liste[c-1], liste[c])
return ret
---snap---
Was man nicht alles tut, um nicht fuer Pruefungen lernen zu muessen
*grummel*
cu boesi
--
Wenn de Lüch net waer un dr Neid #1671 : icq-intern
gäbs lauter glückliche Leid #73628288 : icq-extern
Uhne Lüch un Neid = ganz gewiß boesi111 : aim
wär uf dr Ard is Paradies i171 : reallife
- Vorherige Nachricht: [Python-de] 1,2,3,5,7,8,9 -> "1-3,5,7-9"
- Nächste Nachricht: [Python-de] 1,2,3,5,7,8,9 -> "1-3,5,7-9"
- Nachrichten sortiert nach:
[ Datum ]
[ Thema ]
[ Betreff (Subject)]
[ Autor ]