pythonalgorithmsetintersectionset-intersection

What's the algorithm of 'set.intersection()' in python?


First of all, my purpose is to randomly get only one element in both known sets. So my original method is firstly intersect two sets. And then randomly pick up a element from the intersected set. But this is foolish, because that I only need a elements but a intersected set.

So I need to find the algorithm of set.intersection().

I compare the cost time between the methods of 'set.intersection()' and 'for{for{}}'. Set.intersection() is more faster than other one(100 times). So using 'for{for{}}' to pick up a randomly elements is not a wise idea.

What's the algorithm behind set.intersection() in python?


Solution

  • The algorithm is as follows: the smaller set is looped over and every element is copied depending whether it's found in the bigger set. So, it's the C equivalent of

    def intersect(a, b):
        if len(a) > len(b):
            a, b = b, a
    
        c = set()
        for x in a:
            if x in b:
                c.add(x)
        return c
    

    (Or: return set(x for x in a if x in b).)