Tino Dai wrote:
>     I am wondering about a short cut to doing this. Let's say that we 
> have an array:
> 
> dbs= ['oracle','mysql','postgres','infomix','access']
> 
> and we wanted to do this:
> 
> if 'oracle' in dbs or 'mysql' in dbs or 'bdb' in dbs:
>    <... do something ...>
> 
> Is there a short way or writing this? Something like
>     ('oracle','mysql','bdb') in dbs

Sets to the rescue. Set intersection specifically:

 >>> dbs = set(['oracle','mysql','postgres','infomix','access'])
 >>> mine = set(['oracle','mysql','bdb'])
 >>> dbs & mine
set(['oracle', 'mysql'])
 >>> if dbs & mine:
...     print 'doing something'
...
doing something
 >>>

This also has the advantage of returning to you an object of exactly 
what elements in mine were in dbs. And difference:

 >>> dbs - mine
set(['access', 'infomix', 'postgres'])
 >>>

will show you which elements of mine were not in dbs.

-- 
Sincerely,

Chris Calloway
http://www.seacoos.org
office: 332 Chapman Hall   phone: (919) 962-4323
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599



_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to