I am playing with python and am able to get the intersection of two lists:
result = set(a).intersection(b)
d = [[1,2,3,4], [2,3,4], [3,4,5,6,7]]
for 2.4, you can just define an intersection function.
def intersect(*d): sets = iter(map(set, d)) result = sets.next() for s in sets: result = result.intersection(s) return result
for newer versions of python:
the intersection method takes an arbitrary amount of arguments
result = set(d).intersection(*d[:1])
alternatively, you can intersect the first set with itself to avoid slicing the list and making a copy:
result = set(d).intersection(*d)
I'm not really sure which would be more efficient and have a feeling that it would depend on the size of the
d and the size of the list unless python has an inbuilt check for it like
if s1 is s2: return s1
in the intersection method.
>>> d = [[1,2,3,4], [2,3,4], [3,4,5,6,7]] >>> set(d).intersection(*d) set([3, 4]) >>> set(d).intersection(*d[1:]) set([3, 4]) >>>