Building loop with some exceptions?

Benjamin Kaplan benjamin.kaplan at case.edu
Tue Nov 4 14:54:01 EST 2008


On Tue, Nov 4, 2008 at 2:26 PM, Gilles Ganault <nospam at nospam.com> wrote:

> On Tue, 4 Nov 2008 11:22:27 -0800 (PST), Aaron Brady
> <castironpi 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 :-/
> --
>

domain = xrange(100) # this is all of the numbers
exceptions = [04,34,40,44,48,54,57,67,76,83,99] #this is the stuff you don't
want

new_set = set(domain) - set(exceptions) # removes all of the stuff in
exceptions from the domain
 new_list = sorted(list(new_set)) : turns the set into a list and then sorts
it. Calling sorted ensures that the list is in ascending order
for i in new_list : #new_list just contains the stuff you need.

>
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20081104/ee9359f5/attachment-0001.html>


More information about the Python-list mailing list