Merging lists has made my brain hurt.
Eddie Corns
eddie at holyrood.ed.ac.uk
Wed Oct 2 12:42:46 EDT 2002
More information about the Python-list mailing list
Wed Oct 2 12:42:46 EDT 2002
- Previous message (by thread): Merging lists has made my brain hurt.
- Next message (by thread): Merging lists has made my brain hurt.
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Huw Lynes <huw at moving-picture.com> writes: >Hi All, >I have a list containing an arbitrary number of other lists. The >internal lists contain strings. For example >lol = [ >['aaa', 'bbb', 'ccc'], >['bbb', 'ccc', 'ddd'], >['ccc', 'ddd', 'eee'] >] >I want to merge the three lists into a single list that only contains >the strings present in all three lists. In the above case I want to end >up with >['ccc'] Basically you need to work out what is common between the first two then move on down the rest of the list checking to see what is common between the common list and each of the remaining items, updating the common list at each step. So given a function like: def inter (l1, l2): return [i1 for i1 in l1 if i1 in l2] to return the intersection of two lists you can then use reduce to do all the hard work of applying the first two and so on with: common = reduce (inter, lol) Eddie
- Previous message (by thread): Merging lists has made my brain hurt.
- Next message (by thread): Merging lists has made my brain hurt.
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list