Merging lists has made my brain hurt.

Duncan Booth duncan at rcp.co.uk
Fri Oct 4 10:45:35 EDT 2002


gyromagnetic at excite.com (gyromagnetic) wrote in 
news:4620daca.0210040505.42e47d36 at posting.google.com:

> I caught this thread rather late, but here's another variation 
on the
> theme. Not sure how it compares with the other approaches in 
terms of
> speed, efficiency, etc.
> 
Here is an alternative inspired by my misreading your solution:

>>> def common_entries(lol):
        mst, msk = {}, 1

        for ll in lol:
            for kk in ll:
                mst[kk] = mst.get(kk, 0) | msk
            msk *= 2

        return [ss for ss,i in mst.items() if i+1 == msk]

>>> lol = [
 ['aaa', 'bbb', 'bbb', 'ccc'], ['bbb', 'ccc', 'ddd'], ['ccc', 
'ddd', 'eee']
]
>>> common_entries(lol)
['ccc']




More information about the Python-list mailing list