set1.intersection(set2, set3, ...) trả về set mới chứa các phần tử chung của tất cả các set truyền vào. Không sửa đổi set gốc.
Cú pháp
set1.intersection(*others) -> set # Tương đương: set1 & set2 & set3 ...
Ví dụ
set_1 = {'t','u','t','o','r','i','a','l'}
set_2 = {'p','o','i','n','t'}
set_3 = {'t','u','t'}
# Giao 2 set
print("set1 ∩ set2:", set_1.intersection(set_2))
# Giao 3 set
print("set1 ∩ set2 ∩ set3:", set_1.intersection(set_2, set_3))
Kết quả
set1 ∩ set2: {'i', 'o', 't'}
set1 ∩ set2 ∩ set3: {'t'}
intersection() vs intersection_update()
| Phương thức | Trả về | Sửa đổi set gốc? |
|---|---|---|
| intersection() | Set mới | Không |
| intersection_update() | None | Có (in-place) |