Tuesday, June 17, 2008

Python List Tricks

Poking around looking for ways to clean up my Python code for MWC. It's probably time for me to let go of my 25-year-old BASIC programming experience, and stop making my Python look like BASIC:

Eliminating duplicates from a list using Sets. Currently I convert a list to a dictionary and back to do this:

>>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana']
>>> fruit = set(basket)              # create a set from a list, removing duplicates
>>> fruit
set(['orange', 'pear', 'apple', 'banana'])

>>> li = []                          # create a list from a set
>>> for f in fruit:
...   li.append(f)
... 
>>> li
['orange', 'pear', 'apple', 'banana']

>>> fruit = set([])                   # create an empty set
>>> fruit.add('fred')                 # adding to set
>>> fruit.remove('fred')              # removing from set

>>> fruit.clear()                     # clear all data from a set


>>> 'orange' in fruit                 # fast membership testing
True
>>> 'crabgrass' in fruit
False
Source: Dive into Python.

To do:

  1. Convert MWC keyword (tag) lists to sets - the duplicates annoy me.
  2. Using List Comprehensions as a building block for filters and mapping. What else are they good for?
  3. Filter data using Filters instead of for loops
  4. Add data using Map instead of other crazy structures

No comments: