Subsetting a dictionary

Will Newton will at
Sun Mar 18 23:45:11 CET 2001

I need to get a subset of a dictionary:

D = Dictionary of objects (in this case, training examples for a decision 

D2 = {D | d in D, d under some constraint}

I need to be able to do this, but as it is temporary I do not want to 
destructively remove examples, so:

will return a new dictionary object that is equivalent to D under 
constraint c.

I have code that does this, but it's very slow, consuming typically 75% of 
program runtime - it is called many times, which seems to be unavoidable 
without modifying the algorithm (ID3) too far.

I currently have:

class TrainingData:
         data = {}
        def subset(self, attr, value):
                # Copy semantics
                td = TrainingData()
                for item in
                         if item[attr] != value:

                return td

Can anyone see how I can speed this up?

I could conceivably do this with a list and use filter, I'll see how that 

More information about the Python-list mailing list