python lists have a useful "in" operator to test whether or not an item appears in a list. In partial celebration, I put forward this example:
>>> names = ["Rosalie", "Wheelz", "Watson", "Rosie"]
>>> testNames = ["Sizz", "Wheelz", "Rosalie"]
>>> for aName in testNames:
... if aName in names:
... print aName, " got her thesis done"
... else:
... print aName, " has not been Obified"
...
Sizz has not been Obified
Wheelz got her thesis done
Rosalie got her thesis done
This can also be accomplished with a "set" as in the following example. Sets allow for one to determine unions, differences, symmetrical differences and a variety of other options. They are useful and powerful additions to list objects, for example, to find out who didn't complete a thesis this year one could perform the following
>>> a = set(names)
>>> b = set(testNames)
>>> a
set(['Watson', 'Wheelz', 'Rosie', 'Rosalie'])
>>> b
set(['Sizz', 'Wheelz', 'Rosalie'])
>>> b.difference(a)
set(['Sizz'])
>>>
No comments:
Post a Comment