Subsetting a dictionary

Will Newton will at nospam.misconception.org.uk
Sun Mar 18 17:45:11 EST 2001


I need to get a subset of a dictionary:

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

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:

D.subset(c)
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()
 
                td.data = self.data.copy()
 
                for item in td.data.keys():
                         if item[attr] != value:
                                   del td.data[item]

                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 
works.



More information about the Python-list mailing list