Tham gia hai bộ
Có một số cách để tham gia hai hoặc nhiều bộ trong Python.
Bạn có thể sử dụng phương thức union[]
trả về một bộ mới chứa tất cả các mục từ cả hai bộ hoặc phương thức update[]
để chèn tất cả các mục từ một bộ khác:
Thí dụ
Phương thức union[]
trả về một bộ mới với tất cả các mục từ cả hai bộ:
set1 = {"a", "b", "c"} set2 = {1, 2, 3}
set2 = {1, 2, 3}
set3 = set1.Union [set2] in [set3]
print[set3]
Hãy tự mình thử »
Thí dụ
Phương thức union[]
trả về một bộ mới với tất cả các mục từ cả hai bộ:
set1 = {"a", "b", "c"} set2 = {1, 2, 3}
set2 = {1, 2, 3}
set3 = set1.Union [set2] in [set3]
print[set1]
Hãy tự mình thử »
Phương thức update[]
chèn các mục trong set2 vào set1: Both union[]
and update[]
will exclude any duplicate items.
set1.update [set2] in [set1]
Saad Akbar
Tổng quan
Các bộ có thể được tham gia trong Python theo một số cách khác nhau. Chẳng hạn, update[]
thêm tất cả các yếu tố của một bộ khác. Tương tự, union[]
kết hợp tất cả các yếu tố của hai bộ và trả lại chúng trong một bộ mới. Cả hai hoạt động union[]
và update[]
đều loại trừ các yếu tố trùng lặp.
Ví dụ cho union[]
set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set_union = set1.union[set2]
print[set_union]
Giải trình
Các dòng 1 Vang5: Hoạt động của Liên minh trả về một bộ mới,
7, chứa tất cả các mục từ 2 bộ trước đó,set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
8,set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
9.set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
Ví dụ cho update[]
set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
Giải trình
Các dòng 1 Vang5: Hoạt động của Liên minh trả về một bộ mới,
7, chứa tất cả các mục từ 2 bộ trước đó,set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
8,set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
9.set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
Ví dụ cho update[]
Dòng 1 Vang5: Phương thức cập nhật thêm các giá trị của
9 vàoset1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
8.set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
Phương pháp
3 vàset1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]
4set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]
Ngoài ra còn có một số phương pháp chỉ giữ các bản sao. Đây là set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]
5 và set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]
6.
set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]
set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]
set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]
Giải trình
Các dòng 1 Vang5: Hoạt động của Liên minh trả về một bộ mới,
7, chứa tất cả các mục từ 2 bộ trước đó,set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
8,set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
9.set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
Ví dụ cho update[]
set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set3 = set1.intersection[set2]
print[set3]
Giải trình
Các dòng 1 Vang5: Hoạt động của Liên minh trả về một bộ mới,
7, chứa tất cả các mục từ 2 bộ trước đó,set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
8,set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
9.set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
Ví dụ cho update[]
Dòng 1 Vang5: Phương thức cập nhật thêm các giá trị của
9 vàoset1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
8.set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
Phương pháp
3 vàset1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]
4set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]
Ngoài ra còn có một số phương pháp chỉ giữ các bản sao. Đây là set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]
5 và set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]
6.
set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]
set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]
set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.symmetric_difference_update[set2]
print[set1]
Giải trình
Các dòng 1 Vang5: Hoạt động của Liên minh trả về một bộ mới,
7, chứa tất cả các mục từ 2 bộ trước đó,set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
8,set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
9.set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
Ví dụ cho update[]
set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set3 = set1.symmetric_difference[set2]
print[set3]
Giải trình
Các dòng 1 Vang5: Hoạt động của Liên minh trả về một bộ mới,
7, chứa tất cả các mục từ 2 bộ trước đó,set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
8,set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
9.set1 = {1, 2 , 3}
set2 = {4, 5, 6}
set1.update[set2]
print[set1]
Ví dụ cho update[]
Dòng 1 Vang5: Phương thức cập nhật thêm các giá trị của set1 = {1, 2 , 3} set2 = {4, 5, 6} set1.update[set2] print[set1] set1 = {1, 2 , 3} set2 = {4, 5, 6} set1.update[set2] print[set1]
9 vào
8.
Phương pháp set1 = {"apple", "mango", "cherry"} set2 = {"strawberry", "mango", "apple"} set1.intersection_update[set2] print[set1] set1 = {"apple", "mango", "cherry"} set2 = {"strawberry", "mango", "apple"} set1.intersection_update[set2] print[set1]
3 và
4
Ngoài ra còn có một số phương pháp chỉ giữ các bản sao. Đây là set1 = {"apple", "mango", "cherry"} set2 = {"strawberry", "mango", "apple"} set1.intersection_update[set2] print[set1] set1 = {"apple", "mango", "cherry"} set2 = {"strawberry", "mango", "apple"} set1.intersection_update[set2] print[set1]
5 và
6.
Phương pháp set1 = {"apple", "mango", "cherry"} set2 = {"strawberry", "mango", "apple"} set1.intersection_update[set2] print[set1] set1 = {"apple", "mango", "cherry"} set2 = {"strawberry", "mango", "apple"} set1.intersection_update[set2] print[set1]
3 chỉ lưu trữ các mục có mặt trong cả hai bộ. Trong khi phương thức
4 trả về một bộ mới chứa các mục có mặt trong cả hai bộ.
Ví dụ cho
3set1 = {"apple", "mango", "cherry"}
set2 = {"strawberry", "mango", "apple"}
set1.intersection_update[set2]
print[set1]