Viết chương trình python để tạo tập hợp các tập hợp.

Bạn có gặp khó khăn khi cố gắng sử dụng các thao tác tập hợp Python không?

Trong hướng dẫn này, chúng ta xem xét các thao tác tập hợp trong Python và các thao tác khác được thực hiện trên các tập hợp. Hơn nữa, chúng tôi xem xét các phương thức khác nhau trên tập hợp cũng như các ví dụ về hoạt động tập hợp trong Python. Hãy xem bài viết này để có cái nhìn sâu hơn về tổ hợp với Python

Tập hợp là tập hợp các phần tử không có thứ tự. Mỗi phần tử phải khác biệt và bất biến. Tuy nhiên, bản thân một tập hợp có thể thay đổi

Bạn có thể thêm hoặc xóa các mục khỏi bộ. Bạn cũng có thể thực hiện các phép toán trên chúng, chẳng hạn như hợp, giao và hiệu

Khái niệm về một tập hợp đã được dịch rõ ràng từ toán học sang các ngôn ngữ lập trình như Python. Cùng với nó, một số phương pháp cực kỳ hữu ích đã ra đời, chẳng hạn như

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
0,
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
1 và
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
2, cũng được dịch trực tiếp từ toán học

Tập hợp không chỉ đơn giản là một khái niệm cơ bản trong toán học. Trong suốt sự nghiệp lập trình của mình, bạn có thể sẽ gặp nhiều thách thức có thể được giải quyết nhanh hơn đáng kể bằng cách sử dụng các bộ

Nếu bạn là người mới bắt đầu hoàn toàn với Python, chúng tôi khuyên bạn nên xem bản nhạc này. Nếu bạn là người mới bắt đầu với một số kiến ​​thức về Python, hãy xem khóa học Cơ bản về Python Phần 3, bao gồm các kiến ​​thức cơ bản về biến, danh sách, câu lệnh điều kiện, vòng lặp và hàm

Set và Set Operations trong Python

Một tập hợp được xác định bằng cách bao gồm tất cả các phần tử (i. e. , phần tử) trong dấu ngoặc nhọn và phân tách chúng bằng dấu phẩy hoặc sử dụng phương thức

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
3 tích hợp. Nó có thể bao gồm vô số phần tử thuộc nhiều danh mục khác nhau (số nguyên, số float, bộ, chuỗi, v.v. )

Tuy nhiên, một bộ có thể không chứa các mục có thể thay đổi như danh sách, bộ hoặc từ điển. Truy cập bài viết này để tìm hiểu thêm về sự khác biệt giữa danh sách, bộ dữ liệu và bộ. HọcPython. com là một nền tảng đáng kinh ngạc giúp bạn bắt đầu với Python

Các bộ trống có thể hơi khó sử dụng trong Python. Trong Python, các dấu ngoặc nhọn trống dẫn đến một từ điển trống; . Thay vào đó, chúng ta sử dụng hàm

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
3 không có bất kỳ đối số nào để tạo một tập hợp không có phần tử nào

Xem mã dưới đây để hiểu các khái niệm này

# A set of integers
int_set = {10, 20, 30, 40, 50}

# A set of mixed data types
mixed_set = {9, "This is a set element", (1, 2)}

# All set elements are unique
my_set = {1, 2, 3, 1, 3, 3}
print(my_set) # Output: {1, 2, 3}

# A set can be made from a list
my_set = set([1, 2, 3, 2, 4, 5, 5])
print(my_set) # Output: {1, 2, 3, 4, 5}

Sửa đổi một Set trong Python

Lập chỉ mục và cắt không thể được sử dụng để truy cập hoặc cập nhật một phần tử của tập hợp. Kiểu dữ liệu tập hợp không hỗ trợ nó vì các tập hợp không được sắp xếp

Phương thức

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
5 được sử dụng để thêm một phần tử và phương thức
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
6 được sử dụng để cập nhật nhiều thành phần. Bộ dữ liệu, danh sách, chuỗi và các bộ khác có thể được truyền cho phương thức
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
6. trùng lặp được tránh trong mọi trường hợp

Đoạn mã sau minh họa những ví dụ này

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)

Loại bỏ các phần tử khỏi một tập hợp

Các phương pháp

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
8 và
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
9 được sử dụng để xóa một mục cụ thể khỏi một tập hợp. Chúng giống hệt nhau chỉ có một sự khác biệt. Phương thức
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
8 giữ nguyên tập hợp Nếu phần tử không có trong tập hợp. Mặt khác, phương thức
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
9 đưa ra lỗi nếu phần tử không có trong tập hợp

Việc sử dụng các chức năng này được thể hiện trong ví dụ dưới đây

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
4

Chúng tôi cũng có thể sử dụng phương pháp

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
42 để xóa và trả lại một mặt hàng. Tuy nhiên, không có cách nào để biết mục nào sẽ được bật lên vì tập hợp là kiểu dữ liệu không có thứ tự. Nó hoàn toàn ngẫu nhiên

Lưu ý rằng phương pháp

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
43 được sử dụng để xóa tất cả các phần tử khỏi một tập hợp

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
7

Trong Python, hầu hết, nhưng không phải tất cả, các thao tác thiết lập được thực hiện theo một trong hai cách. bởi một nhà điều hành hoặc bởi một phương pháp. Trước khi chúng ta xem xét các hoạt động tập hợp khác nhau hoạt động như thế nào trong Python, điều quan trọng là phải hiểu sự khác biệt giữa toán tử và phương thức

Trong Python, một phương thức tương tự như một hàm ngoại trừ nó được gắn với một đối tượng. Khi chúng ta gọi một phương thức trên một đối tượng, nó có thể ảnh hưởng hoặc không ảnh hưởng đến đối tượng đó – trong tình huống này, một tập hợp. Điều đáng chú ý là mỗi toán tử tương ứng với một hàm đặc biệt của Python. Vì vậy, cả hai đều hoàn thành cùng một việc nhưng có các yêu cầu cú pháp riêng biệt

Python hỗ trợ nhiều phép toán tập hợp, bao gồm hợp, giao, hiệu và hiệu đối xứng. Chúng ta hãy xem xét một số ví dụ về hoạt động thiết lập trong Python

Hoạt động của Liên minh Python với ví dụ

Hợp của hai tập hợp là tập hợp gồm tất cả các phần tử, không trùng nhau, nằm trong một trong hai hoặc cả hai tập hợp. Trong Python, bạn có thể sử dụng phương thức

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
0 hoặc cú pháp
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
45 để tìm phép hợp. Hãy xem một ví dụ về liên minh Python

Sử dụng toán tử

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
45

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
1

Chạy đoạn mã trên tạo ra hai bộ.

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
47 và
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
48. Sau đó, toán tử hợp tạo một
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
49 với tất cả các phần tử duy nhất từ ​​
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
47 và
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
48

Điều tương tự cũng đạt được bằng cách sử dụng phương pháp

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
0

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
8

Vì hợp bao gồm các phần tử của cả hai tập hợp nên nó là đối xứng. Vì vậy, kết quả của

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
73 trong cùng một tập hợp với
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
74

Hoạt động giao cắt Python với ví dụ

Giao của hai tập hợp là tập hợp có tất cả các phần tử chung của cả hai tập hợp. Trong Python, bạn có thể sử dụng phương thức

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
1 hoặc toán tử & để tìm giao điểm. Dưới đây là một số ví dụ về giao điểm Python

Sử dụng toán tử &

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
2

Chạy đoạn mã trên tạo ra hai bộ.

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
47 và
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
48. Sau đó, toán tử giao nhau tạo một
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
49 với tất cả các phần tử duy nhất từ ​​
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
47 và
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
48

Điều tương tự đạt được bằng cách sử dụng phương pháp

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
1

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
9

Vì phương pháp giao nhau tạo ra một tập hợp các phần tử chung cho cả hai tập hợp, nên nó là phương pháp đối xứng. Vì vậy, kết quả của

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
12 trong cùng một tập hợp với
# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
13

Thao tác đặt chênh lệch Python với ví dụ

Hiệu của hai tập hợp là tập hợp gồm tất cả các phần tử có trong tập hợp thứ nhất nhưng không có trong tập hợp thứ hai. Python cho phép bạn sử dụng phương thức

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
2 hoặc toán tử - để thực hiện việc này. Hãy xem xét một số ví dụ về sự khác biệt của bộ Python

Sử dụng toán tử -

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
3

Bạn cũng có thể sử dụng phương pháp

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
2

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
5

Như trong ví dụ, toán tử khác biệt không đối xứng. Bộ nào bạn đặt tên đầu tiên quan trọng và ảnh hưởng đến kết quả của

# Initialize a set
my_set = {11, 60}

# Add an element to the set
# Output: {11, 21, 60}
my_set.add(21)
print(my_set)

# Add more than one element to the set
# Output: {8, 11, 13, 20, 21, 60}
my_set.update([20, 13, 8])
print(my_set)
49

Sử dụng Python Set Operations

Trong hướng dẫn này, bạn đã học cách định nghĩa các thao tác tập hợp trong Python. Ngoài ra, chúng ta đã làm quen với các hàm, toán tử và phương thức được sử dụng để làm việc với các tập hợp. Nếu bạn muốn tìm hiểu thêm về các bộ Python, e. g. , làm thế nào để có được sự khác biệt đối xứng, hãy truy cập bài viết “Python Set Operations và hơn thế nữa. Tất cả những gì bạn cần biết về Python Sets. ”