Yvan Needs Comments - 2006-09-12 04:32:17

Yvan mentioned a reasonably common mistake in python, the wonderful lets do a linear search through a list of keys instead of using the dict like it was intended trick.

I can't help but mentioning though, that if you have a non-ancient python you can skip has_key and just use:

        if feature not in setHash:
            setHash[feature] = []
        setHash[feature].append(featureSet)

And in fact depending on how you define readability there's (be honest there's no way you're using python 1.6 or earlier):

        setHash.setdefault(feature,[]).append(featureSet)

Or for the pythoners who wish they were coding in perl the entire snippet of Yvan's can be replaced with:

        setHash = {}
        [setHash.setdefault(y,[]).append(x) for x in featureSets for y in x]

But that has all the drawbacks of perl's "map in a void context".

List All | Archives: 2002 2003 2004 2005 2006 2007 2008