Search code examples
pythonstringoutput

Creating two lists and compare the matching


I have two list of numbers but I need a evaluate and see if any numbers match then out put the # of matching numbers.

import random
matches = random.sample (range(1, 20), 5),random.sample (range(1, 20), 5)

Solution

  • You might be able to use a set intersection.

    from random import sample
    
    set_a = set(sample(range(0, 50), 10))
    set_b = set(sample(range(0, 50), 10))
    
    print set_a.intersection(set_b) # [3, 4]
    print set_a & set_b # sugar for the same thing