I'm trying to write a program that finds the intersection of two sequences.
The program then generates a list of all the elements of the intersection of the two sequences (in any order), taking into account the replication of the values. This means that if a value 'i' appears twice in the first sequence and three times in the second sequence, then the intersection should contain two instances of 'I'
here is my code:
print("This program finds the intersection of two sets")
sequenceOne = input("Please enter the space-separated elements of the first set: ")
sequenceTwo = input("Please enter the space-separated elements of the second set: ")
sequenceOne = sequenceOne.split()
sequenceTwo = sequenceTwo.split()
listOfIntersection = []
for i in sequenceOne:
if i in sequenceTwo:
listOfIntersection.append(i)
print(listOfIntersection)
input:
sequence One: 12 k e 34 1.5 12 hi 12 0.2
sequence Two:1.5 hi 12 0.1 54 12 hi hi hi
My output:
['12', '1.5', '12', 'hi', '12']
Desired output:
{12, 12, 1.5, hi}
how do I get only the numbers that occur in both sequences even if its the same number evenly? (I hope this makes sense). also how to get the curly brackets?
thank you.