You should use generator expressions here, they do lazy evaluation and save a lot of memory:
In [46]: from itertools import imap
In [47]: a = [[1,2,3], [2,3,4], [3,4,5]]
In [48]: reduce(set.intersection,imap(set,a))
Out[48]: set([3])
considering your file looks like this:
1 2 3
2 3 4
3 4 5
code:
Using itertools.combinations()
:
with open("abc.txt") as f:
lines=(map(int,x.split()) for x in f)
for x in combinations(lines,2):
print x,'-->',reduce(set.intersection,imap(set,x))
....:
([1, 2, 3], [2, 3, 4]) --> set([2, 3])
([1, 2, 3], [3, 4, 5]) --> set([3])
([2, 3, 4], [3, 4, 5]) --> set([3, 4])