Which is faster? (if not b in m) or (if m.count(b) > 0)

Farel c_shyone at yahoo.com
Tue Feb 14 23:14:02 EST 2006


Which is Faster in Python and Why?

jc = {}; m = []
x = [ [1,2,3,4,5,6,7,8,9],[..],.......] # upwards of 10000 entries

def mcountb():
    for item in x:
        b = item[:]; b.sort(); bc = 0
        for bitem in b: bc += int(bitem)
        try: m = jc[bc]
        except: m = []
        if m.count(b) == 0:
             m.append(b); jc[bc]=m

def binm()
    for item in x:
        b = item[:]; b.sort(); bc = 0
        for bitem in b: bc += int(bitem)
        try: m = jc[bc]
        except: m = []
        if not b in m:
            m.append(b); jc[bc]=m




More information about the Python-list mailing list