set intersections and python

Status
Not open for further replies.

JTM

Ars Praetorian
401
Hi,
I am trying to learn Python. I have an O'Reilly book called Introducing Python. It talks about Python's implementation of sets and gives an example of set intersections. The set intersections example also uses a dictionary. The code looks like the following

Code:
>>> drinks = {
... 'martini':{'vodka','vermouth'},
... 'black russian':{'vodka', 'kahlua'},
... 'white russian':{'cream','kahlua','vodka'},
... 'manhattan':{'rye','vermouth','bitters'},
... 'screwdriver':{'orange juice','vodka'}
... }
>>> for name,contents in drinks.items():
...     if contents & {'vermouth','orange juice'}:
...         print(name)
...
screwdriver
manhattan
martini

The code above comes from the book and is what I typed into the Python interpreter. I haven't thought too much about set intersections.

Is this code basically saying, return contents of a drink if they contain EITHER vermouth OR orange juice?

How would I rewrite this to return contents if they contain BOTH vermouth AND orange juice?

I tried:

Code:
if (contents & {'vermouth'}) && (contents & {'orange juice'}):

and the python interpreter tells me that is invalid syntax.

Thanks,

Jason
 
Status
Not open for further replies.