Building loop with some exceptions?

MRAB google at mrabarnett.plus.com
Tue Nov 4 18:53:18 EST 2008


On Nov 4, 10:02 pm, Aaron Brady <castiro... at gmail.com> wrote:
> On Nov 4, 1:26 pm, Gilles Ganault <nos... at nospam.com> wrote:
>
> > On Tue, 4 Nov 2008 11:22:27 -0800 (PST), Aaron Brady
>
> > <castiro... at gmail.com> wrote:
> > >> for i=01 to 99 except 04, 34, 40, 44, 48, 54, 57, 67, 76, 83, 89:
>
> > >sorted( list( set( domain ) - set( exceptions ) ) )
>
> > >Set subtraction.
>
> > Thanks a lot but... I don't know what the above means :-/
>
> This example produces the numbers 0..9, except 3, 5, and 9.
>
> >>> sorted( list( set( range( 10 ) ) - set( [ 3, 5, 9 ] ) ) )
>
> [0, 1, 2, 4, 6, 7, 8]

FYI, 'set' accepts an iterable, so you don't need 'list':

>>> sorted(set(range(10)) - set([3, 5, 9]))
[0, 1, 2, 4, 6, 7, 8]



More information about the Python-list mailing list