Merging lists has made my brain hurt.

Max M maxm at mxm.dk
Wed Oct 2 18:24:28 EDT 2002


Max M wrote:

> Or the shortest one I can come up with. Don't know about the brain-pain 
> though ;-)
> 
> d = {}; common = []
> for i in reduce(lambda x,y: x+y, lol):
>     d[i] = d.setdefault(i, 0) + 1
>     if d[i] == len(lol): common.append(i)
> print common

well I found a shorter one (I can't help it. these short snippets are 
like a good puzzle):

l = reduce(lambda x,y:x+y, lol)
l.sort()
common = [l[i] for i in range(len(l)) if l[i:i+len(lol)] == len(lol)*[l[i]]]
print common

Not nice to the brain, nor very efficient I guess. But fun.


;-)

regards Max M




More information about the Python-list mailing list