views:

147

answers:

2

I am playing with python and am able to get the intersection of two lists:

result = set(a).intersection(b)

Now if d is a list containing a and b and a third element c, is there an built-in function for finding the intersection of all the three lists inside d? So for instance,

d = [[1,2,3,4], [2,3,4], [3,4,5,6,7]]

then the result should be

[3,4]
+2  A: 

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[0]).intersection(*d[:1])

alternatively, you can intersect the first set with itself to avoid slicing the list and making a copy:

result = set(d[0]).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[0] 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[0]).intersection(*d)
set([3, 4])
>>> set(d[0]).intersection(*d[1:])
set([3, 4])
>>> 
aaronasterling
@AaronMcSmooth: It gives me `AttributeError: 'list' object has no attribute 'intersection'` if I do that. Am I missing something?
Legend
@Legend. you have to map it to a set first. I somehow missed the fact that they were lists. After that, you can just pass lists (or any other iterable) to the `intersection` method
aaronasterling
@AaronMcSmooth: Actually, not sure why but I'm getting this error no matter what solution I try: `TypeError: intersection() takes exactly one argument (3 given)`
Legend
@Legend. both my answer and TokenMacGuy's work for me on python 2.6 and 3.1
aaronasterling
@AaronMcSmooth: Aah... Is there some solution for Python 2.4 by any chance?
Legend
@AaronMcSmooth: Today's a bad day. Sorry. When I define the intersection, it gives me: `UnboundLocalError: local variable 'set' referenced before assignment` I don't understand why it is considering set as a local variable.
Legend
@AaronMcSmooth: I edited your answer to something that works. Thanks for the help.
Legend
@Legend. Thanks for cleaning up my post. but remember: 'Thus spoke the Lord: "Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou then proceed to four. Tabs are the devil's apples!"'
aaronasterling
@AaronMcSmooth: Aye aye captain! Thou shall remember that for next time... Thanks! :)
Legend
Lobbeth thou thy holy space bar of Antioch.
intuited
+1  A: 
set.intersection(*map(set,d))
TokenMacGuy
@TokenMacGuy: Not sure what's wrong here. Now it gives me: `TypeError: intersection() takes exactly one argument (2 given)`
Legend
@Legend, what version of python are you on? it works for me.
aaronasterling
@AaronMcSmooth: 2.4.3?
Legend
@TokenMacGuy: Thanks a lot. +1 for that. I just couldn't use it because of my Python version :(
Legend