Removing items from a set – remove(), pop(), and difference
Python gives us several ways to remove items from a set
collection. We can use the remove()
method to remove a specific item. We can use the pop()
method to remove an arbitrary item.
Additionally, we can compute a new set using set intersection, difference, and symmetric difference operators: &
, -
, and ^
. These will produce a new set which is a subset of a given input set.
Getting ready
Sometimes we'll have log files that contain lines with complex and varied formats. Here's a small snippet from a long, complex log:
>>> log = '''
... [2016-03-05T09:29:31-05:00] INFO: Processing ruby_block[print IP] action run (@recipe_files::/home/slott/ch4/deploy.rb line 9)
... [2016-03-05T09:29:31-05:00] INFO: Installed IP: 111.222.111.222
... [2016-03-05T09:29:31-05:00] INFO: ruby_block[print IP] called
...
... - execute the ruby block print IP
... [2016-03-05T09:29:31-05:00] INFO: Chef Run complete in 23.233811181 seconds
....