Performance problem with filtering

Gerhard Häring gh_pythonlist at gmx.de
Wed Mar 13 23:50:53 EST 2002


Le 14/03/02 ? 05:08, Gerhard H?ring écrivit:
> Anybody wrote a subclassed list yet that is optimized for __contains__?

I just tried that by subclassing list in Python 2.2, but it turned out that I'd
need to override almost all methods in order to not introduce any bugs.

So I just built this minimal class which does exactly what I need here:

class FastList:
    def __init__(self, l = None):
        self.count = {}
        self.l = []
        if l is not None:
            for item in l:
                self.append(item)

    def append(self, item):
        self.count[item] = self.count.get(item, 0) + 1
        self.l.append(item)

    def __contains__(self, item):
        return self.count.has_key(item)

    def __getitem__(self, pos):
        return self.l[pos]

Gerhard
-- 
This sig powered by Python!
Außentemperatur in München: 7.4 °C      Wind: 1.1 m/s




More information about the Python-list mailing list