[Tutor] Skipping elements from inside generator loops
Scot W. Stevenson
scot@possum.in-berlin.de
Sun, 4 Aug 2002 15:17:05 +0200
Hello Gregor,
> >>> zip([None]+p,p)
> [(None, 'black cat'), ('black cat', 'white cat'), ('white cat', 'grey
> cat'),
> ('grey cat', 'ALARM!'), ('ALARM!', 'the evil dog'), ('the evil dog',
> 'fat cat'),
> ('fat cat', 'slow cat')]
> the second Element of each pair is good if only 'ALARM!' does not occur
> in the pair:
> >>> [pair[1] for pair in zip([None]+p,p) if 'ALARM!' not in pair]
> ['black cat', 'white cat', 'grey cat', 'fat cat', 'slow cat']
> Not very elegant! Amendments?
This is /sehr beeindruckend/ - and it even works if 'ALARM!' ist the first
entry. I really like the idea of shifting the first part of the zip call
with one "None" to produce pairs; this is not something I would have
thought of...
Thank you for the demonstration, I'll have to go play around with it for a
while =8).
Y, Scot
--
Scot W. Stevenson -- scot@possum.in-berlin.de -- Zepernick, Germany