[Pythonmac-SIG] If/else vs or

Chris Weisiger cweisiger at msg.ucsf.edu
Fri Oct 29 18:31:34 CEST 2010


Or you could use actual sets:

>>> colors = set(['red', 'green', 'blue', 'orange', 'fuscia', 'black',
'white'])
>>> subset = set(['red', 'green', 'blue', 'purple'])
>>> subset.intersection(colors)
set(['blue', 'green', 'red'])

Of course, this loses your ordering, but it's otherwise far easier to read
than a list comprehension.

-Chris

On Fri, Oct 29, 2010 at 9:19 AM, Dan Ross <dan at rosspixelworks.com> wrote:

> Indeed. That's awfully nice and concise.
>
> On Fri, 29 Oct 2010 09:14:06 -0700, Christopher Barker
> <Chris.Barker at noaa.gov> wrote:
> > On 10/29/10 7:56 AM, Dan Ross wrote:
> >  > I've been trying to use more list comprehensions recently.
> >
> > ahh -- then you want something like:
> >
> > In [15]: colors =
> ['red','green','blue','orange','fuchsia','black','white']
> >
> > In [16]: subset = ['red','green','blue','purple']
> >
> > In [17]: [c for c in colors if c in subset]
> >
> > Out[17]: ['red', 'green', 'blue']
> >
> >
> > (so much for one obvious way to do it!)
> >
> > -Chris
> _______________________________________________
> Pythonmac-SIG maillist  -  Pythonmac-SIG at python.org
> http://mail.python.org/mailman/listinfo/pythonmac-sig
> unsubscribe: http://mail.python.org/mailman/options/Pythonmac-SIG
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pythonmac-sig/attachments/20101029/9997b71e/attachment.html>


More information about the Pythonmac-SIG mailing list