intersection python code example
Example 1: intersection python
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.intersection(y)
print(z)
Example 2: python check if two sets intersect
a = [1, 2, 3]
b = [3, 4, 5]
bool(set(a) & set(b))
Example 3: intersection of lists in python
import numpy as np
recent_coding_books = np.intersect1d(recent_books,coding_books)
Example 4: list intersection python
name1 =list("".join(str(x)for x in input("Enter name1").replace(" ","")))
name2 =list("".join(str(x)for x in input("Enter name2").replace(" ","")))
common = [x for x in name1 if x in name2]
unique = set(common)
d=0
for x in unique:
d = d + min(name1.count(x),name2.count(x))
difference = (len(name1) + len(name2)) - d*2
print(difference)
Example 5: python set intersection
both = {'a', 'A', 'b', 'B'}
some = {'a', 'b', 'c'}
result1 = both & some
result2 = both.intersection(some)
print(result1 == result2)