wiki:LambdaSetOperations

Version 1 (modified by bsoher, 10 years ago) (diff)

--

Lambda operators to perform simple set operators

        #Example of lambda for simple list AND, OR, XOR, UNION 
        list1=range(5) # [0,1,2,3,4]
        list2=range(3,7) # [3,4,5,6]
        union=list1+filter(lambda x:x not in list1,list2)
        # union = [0,1,2,3,4,5,6]
        
        Intersection is just as easy
        intersection=filter(lambda x:x in list1,list2)
        # intersection=[3,4]

        Difference is the last thing you wanted...
        difference=filter(lambda x:x not in list2,list1)
        # difference=[0,1,2]
        
        And my set vocabulary is rusty but the distinct elements or those from both lists that are NOT in common (un-intersection?) would be:
        distinct=filter(lamba x:x not in list2,list1)+filter(lambda x:x not in list1,list2)
        # distinct=[0,1,2,5,6]