Tôi đang cố gắng tạo một từ điển có các giá trị như một đối tượng đã đặt. Tôi muốn một bộ sưu tập các tên độc đáo liên quan đến một tài liệu tham khảo duy nhất]. Mục đích của tôi là cố gắng và tạo ra một cái gì đó như:
AIM:
Dictionary[key_1] = set['name']
Dictionary[key_2] = set['name_2', 'name_3']
Thêm vào SET:
Dictionary[key_2].add['name_3']
Tuy nhiên, sử dụng đối tượng SET phá chuỗi
Dictionary[key_2].add['name_3']
4 thành các ký tự được mong đợi như được hiển thị ở đây. Tôi đã cố gắng biến chuỗi thành một tuple, tức là Dictionary[key_2].add['name_3']
5 và Dictionary[key_2].add['name_3']
6, nhưng điều này không hoạt động theo yêu cầu vì chuỗi được chia thành các ký tự.Là cách duy nhất để thêm một chuỗi vào một tập hợp thông qua danh sách để dừng nó bị chia thành các ký tự như
'n', 'a', 'm', 'e'
Bất kỳ ý tưởng khác sẽ được nhận biết rất biết ơn.
Jonrsharpe
Huy hiệu vàng 111K2525 gold badges217 silver badges403 bronze badges
hỏi ngày 9 tháng 7 năm 2014 lúc 12:50Jul 9, 2014 at 12:50
2
Bạn có thể viết một tuple phần tử duy nhất như @LarsMans giải thích, nhưng thật dễ dàng để quên dấu phẩy. Nó có thể ít bị lỗi nếu bạn chỉ sử dụng danh sách làm tham số cho hàm tạo và phương thức đã đặt:
Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
Tất cả nên làm việc theo cách bạn mong đợi.
Đã trả lời ngày 9 tháng 7 năm 2014 lúc 13:24Jul 9, 2014 at 13:24
DuncanduncanDuncan
88.7K11 Huy hiệu vàng117 Huy hiệu bạc155 Huy hiệu Đồng11 gold badges117 silver badges155 bronze badges
2
Dictionary[key_2].add['name_3']
7 không phải là một tuple. Nó chỉ là biểu thức Dictionary[key_2].add['name_3']
8, dấu ngoặc đơn. Một tuple một yếu tố được viết Dictionary[key_2].add['name_3']
9; Một danh sách một phần tử 'n', 'a', 'm', 'e'
0 đẹp hơn và cũng hoạt động.Trong Python 2.7, 3.x bạn cũng có thể viết
'n', 'a', 'm', 'e'
1 để xây dựng một tập hợp.Đã trả lời ngày 9 tháng 7 năm 2014 lúc 12:53Jul 9, 2014 at 12:53
Fred Foofred FooFred Foo
348K73 Huy hiệu vàng726 Huy hiệu bạc824 Huy hiệu Đồng73 gold badges726 silver badges824 bronze badges
Sự khác biệt giữa hai danh sáchSet is an unordered collection of data types that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set.
Python đặt để kiểm tra xem chuỗi có phải là panagram không
Các hoạt động của Python Set [Liên minh, Giao lộ, Sự khác biệt và sự khác biệt đối xứng]set[] function with an iterable object or a sequence by placing the sequence inside curly braces, separated by a ‘comma’.
Chuỗi được nối với các ký tự không phổ biến trong PythonA set cannot have mutable elements like a list or dictionary, as it is mutable.
Python3
'n', 'a', 'm', 'e'
2'n', 'a', 'm', 'e'
3 'n', 'a', 'm', 'e'
4'n', 'a', 'm', 'e'
5'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7'n', 'a', 'm', 'e'
8'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1'n', 'a', 'm', 'e'
2'n', 'a', 'm', 'e'
3 'n', 'a', 'm', 'e'
4'n', 'a', 'm', 'e'
7Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
6'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}0
'n', 'a', 'm', 'e'
9
'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}4
'n', 'a', 'm', 'e'
3 Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}6
'n', 'a', 'm', 'e'
2'n', 'a', 'm', 'e'
3 'n', 'a', 'm', 'e'
4Set with the use of Numbers: {1, 2, 3, 4, 5, 6} Set with the use of Mixed Values {1, 2, 'For', 4, 6, 'Geeks'}0
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Set with the use of Numbers: {1, 2, 3, 4, 5, 6} Set with the use of Mixed Values {1, 2, 'For', 4, 6, 'Geeks'}3
'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1'n', 'a', 'm', 'e'
2'n', 'a', 'm', 'e'
3 'n', 'a', 'm', 'e'
4Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}0
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}1
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}3
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}1
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}6
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}9
'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1Đầu ra
Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}
Một tập hợp chỉ chứa các phần tử duy nhất nhưng tại thời điểm tạo tập hợp, nhiều giá trị trùng lặp cũng có thể được truyền. Thứ tự của các phần tử trong một bộ không được xác định và không thể thay đổi. Loại phần tử trong một tập hợp không cần phải giống nhau, các giá trị loại dữ liệu hỗn hợp khác nhau cũng có thể được chuyển cho tập hợp. & NBSP;
Python3
Các
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Initial Set: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} Set after Removal of two elements: {1, 2, 3, 4, 7, 8, 9, 10, 11, 12} Set after Discarding two elements: {1, 2, 3, 4, 7, 10, 11, 12} Set after Removing a range of elements: {7, 10, 11, 12}7
'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1Các
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Dictionary[key_2].add['name_3']
21'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1Đầu ra
Set with the use of Numbers: {1, 2, 3, 4, 5, 6} Set with the use of Mixed Values {1, 2, 'For', 4, 6, 'Geeks'}
Một tập hợp chỉ chứa các phần tử duy nhất nhưng tại thời điểm tạo tập hợp, nhiều giá trị trùng lặp cũng có thể được truyền. Thứ tự của các phần tử trong một bộ không được xác định và không thể thay đổi. Loại phần tử trong một tập hợp không cần phải giống nhau, các giá trị loại dữ liệu hỗn hợp khác nhau cũng có thể được chuyển cho tập hợp. & NBSP;
Python3
Các
'n', 'a', 'm', 'e'
6Dictionary[key_2].add['name_3']
35Các
Tạo một tập hợp với một phương thức khác
Dictionary[key_2].add['name_3']
25'n', 'a', 'm', 'e'
3 Dictionary[key_2].add['name_3']
27Set after Addition of elements using Update: {4, 5, [6, 7], 10, 11}7
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
Set after Addition of elements using Update: {4, 5, [6, 7], 10, 11}9
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
Initial set {'For', 'Geeks'} Elements of set: For Geeks True555133add[] function. Only one element at a time can be added to the set by using add[] method, loops are used to add multiple elements at a time with the use of add[] method.
Thêm các phần tử vào một tập hợp Lists cannot be added to a set as elements because Lists are not hashable whereas Tuples can be added because tuples are immutable and hence Hashable.
Python3
'n', 'a', 'm', 'e'
2'n', 'a', 'm', 'e'
3 'n', 'a', 'm', 'e'
4'n', 'a', 'm', 'e'
5'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7'n', 'a', 'm', 'e'
8'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1Dictionary[key_2].add['name_3']
46Dictionary[key_2].add['name_3']
47'n', 'a', 'm', 'e'
9Dictionary[key_2].add['name_3']
46Dictionary[key_2].add['name_3']
50'n', 'a', 'm', 'e'
9Dictionary[key_2].add['name_3']
52Initial Set: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} Set after Removal of two elements: {1, 2, 3, 4, 7, 8, 9, 10, 11, 12} Set after Discarding two elements: {1, 2, 3, 4, 7, 10, 11, 12} Set after Removing a range of elements: {7, 10, 11, 12}1
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
Dictionary[key_2].add['name_3']
55Dictionary[key_2].add['name_3']
56'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Dictionary[key_2].add['name_3']
59'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1Sử dụng phương thức ADD []
Dictionary[key_2].add['name_3']
72Dictionary[key_2].add['name_3']
73'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Dictionary[key_2].add['name_3']
76'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1
Đầu ra
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}
Một tập hợp chỉ chứa các phần tử duy nhất nhưng tại thời điểm tạo tập hợp, nhiều giá trị trùng lặp cũng có thể được truyền. Thứ tự của các phần tử trong một bộ không được xác định và không thể thay đổi. Loại phần tử trong một tập hợp không cần phải giống nhau, các giá trị loại dữ liệu hỗn hợp khác nhau cũng có thể được chuyển cho tập hợp. & NBSP;
Các
Python3
Các
Tạo một tập hợp với một phương thức khác
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Dictionary[key_2].add['name_3']
99'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1Đầu ra
Set after Addition of elements using Update: {4, 5, [6, 7], 10, 11}
Một tập hợp chỉ chứa các phần tử duy nhất nhưng tại thời điểm tạo tập hợp, nhiều giá trị trùng lặp cũng có thể được truyền. Thứ tự của các phần tử trong một bộ không được xác định và không thể thay đổi. Loại phần tử trong một tập hợp không cần phải giống nhau, các giá trị loại dữ liệu hỗn hợp khác nhau cũng có thể được chuyển cho tập hợp. & NBSP;
Các
Python3
'n', 'a', 'm', 'e'
2'n', 'a', 'm', 'e'
3 'n', 'a', 'm', 'e'
4Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}0
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}1
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}3
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}1
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}6
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7'n', 'a', 'm', 'e'
15'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7'n', 'a', 'm', 'e'
21'n', 'a', 'm', 'e'
9Đầu ra
Dictionary[key_2].add['name_3']
72'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
29'n', 'a', 'm', 'e'
3'n', 'a', 'm', 'e'
31'n', 'a', 'm', 'e'
9Một tập hợp chỉ chứa các phần tử duy nhất nhưng tại thời điểm tạo tập hợp, nhiều giá trị trùng lặp cũng có thể được truyền. Thứ tự của các phần tử trong một bộ không được xác định và không thể thay đổi. Loại phần tử trong một tập hợp không cần phải giống nhau, các giá trị loại dữ liệu hỗn hợp khác nhau cũng có thể được chuyển cho tập hợp. & NBSP;
Đầu ra
Initial set {'For', 'Geeks'} Elements of set: For Geeks True
Một tập hợp chỉ chứa các phần tử duy nhất nhưng tại thời điểm tạo tập hợp, nhiều giá trị trùng lặp cũng có thể được truyền. Thứ tự của các phần tử trong một bộ không được xác định và không thể thay đổi. Loại phần tử trong một tập hợp không cần phải giống nhau, các giá trị loại dữ liệu hỗn hợp khác nhau cũng có thể được chuyển cho tập hợp. & NBSP;
Các
Các
Python3
Tạo một tập hợp với một phương thức khác
Dictionary[key_2].add['name_3']
25'n', 'a', 'm', 'e'
3 Dictionary[key_2].add['name_3']
27Set after Addition of elements using Update: {4, 5, [6, 7], 10, 11}7
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
Set after Addition of elements using Update: {4, 5, [6, 7], 10, 11}9
Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
Initial set {'For', 'Geeks'} Elements of set: For Geeks True555133
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7'n', 'a', 'm', 'e'
69'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1'n', 'a', 'm', 'e'
73Initial Set: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} Set after Removal of two elements: {1, 2, 3, 4, 7, 8, 9, 10, 11, 12} Set after Discarding two elements: {1, 2, 3, 4, 7, 10, 11, 12} Set after Removing a range of elements: {7, 10, 11, 12}3
'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
73Initial Set: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} Set after Removal of two elements: {1, 2, 3, 4, 7, 8, 9, 10, 11, 12} Set after Discarding two elements: {1, 2, 3, 4, 7, 10, 11, 12} Set after Removing a range of elements: {7, 10, 11, 12}1
'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7'n', 'a', 'm', 'e'
81'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1'n', 'a', 'm', 'e'
85Dictionary[key_2].add['name_3']
47'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
85Dictionary[key_2].add['name_3']
50'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7'n', 'a', 'm', 'e'
93'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1Dictionary[key_2].add['name_3']
63 Dictionary[key_2].add['name_3']
64Dictionary[key_2].add['name_3']
65 Dictionary[key_2].add['name_3']
66'n', 'a', 'm', 'e'
7__777662Dictionary[key_2].add['name_3']
72Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
07'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
10'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1Đầu ra
Initial Set: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} Set after Removal of two elements: {1, 2, 3, 4, 7, 8, 9, 10, 11, 12} Set after Discarding two elements: {1, 2, 3, 4, 7, 10, 11, 12} Set after Removing a range of elements: {7, 10, 11, 12}
Sử dụng phương thức pop []:
Hàm pop [] cũng có thể được sử dụng để xóa và trả về một phần tử khỏi tập hợp, nhưng nó chỉ xóa phần tử cuối cùng của tập hợp. & NBSP;
Lưu ý: Nếu tập hợp không được đặt hàng thì không có cách nào để xác định phần tử nào được bật bằng cách sử dụng hàm pop []. & Nbsp;If the set is unordered then there’s no such way to determine which element is popped by using the pop[] function.
Python3
'n', 'a', 'm', 'e'
2'n', 'a', 'm', 'e'
3 'n', 'a', 'm', 'e'
4Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}0__777762
'n', 'a', 'm', 'e'
54Dictionary[key_2].add['name_3']
55Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
Dictionary[key_2].add['name_3']
47Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
Dictionary[key_2].add['name_3']
50Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
Dictionary[key_2].add['name_3']
93Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
Dictionary[key_2].add['name_3']
95Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2
'n', 'a', 'm', 'e'
65Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}6
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7'n', 'a', 'm', 'e'
69'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
49'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
52'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1Đầu ra
Dictionary[key_2].add['name_3']
0Sử dụng phương thức pop []:
Hàm pop [] cũng có thể được sử dụng để xóa và trả về một phần tử khỏi tập hợp, nhưng nó chỉ xóa phần tử cuối cùng của tập hợp. & NBSP;
Python3
Lưu ý: Nếu tập hợp không được đặt hàng thì không có cách nào để xác định phần tử nào được bật bằng cách sử dụng hàm pop []. & Nbsp;
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
72'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
76'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
79'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Dictionary[key_1] = set[['name']]
Dictionary[key_2] = set[['name_2', 'name_3']]
Dictionary[key_2].add[['name_3']]
1Đầu ra
Dictionary[key_2].add['name_3']
1Sử dụng phương thức pop []: in Python are immutable objects that only support methods and operators that produce a result without affecting the frozen set or sets to which they are applied. While elements of a set can be modified at any time, elements of the frozen set remain the same after creation.
Hàm pop [] cũng có thể được sử dụng để xóa và trả về một phần tử khỏi tập hợp, nhưng nó chỉ xóa phần tử cuối cùng của tập hợp. & NBSP;
Python3
Lưu ý: Nếu tập hợp không được đặt hàng thì không có cách nào để xác định phần tử nào được bật bằng cách sử dụng hàm pop []. & Nbsp;
'n', 'a', 'm', 'e'
2'n', 'a', 'm', 'e'
3 'n', 'a', 'm', 'e'
4Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}0__777762
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}08
'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}11
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}14
'n', 'a', 'm', 'e'
9'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}04
Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}19
Đầu ra
Dictionary[key_2].add['name_3']
2Sử dụng phương thức rõ ràng []:
Để loại bỏ tất cả các phần tử khỏi hàm, clear [] được sử dụng. & Nbsp;
'n', 'a', 'm', 'e'
2'n', 'a', 'm', 'e'
3 'n', 'a', 'm', 'e'
4Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}0
Set after Addition of elements using Update: {4, 5, [6, 7], 10, 11}77____253
Set after Addition of elements using Update: {4, 5, [6, 7], 10, 11}9
'n', 'a', 'm', 'e'
53Initial set {'For', 'Geeks'} Elements of set: For Geeks True5
'n', 'a', 'm', 'e'
53__Các bộ đông lạnh trong Python là các đối tượng bất biến chỉ hỗ trợ các phương thức và toán tử tạo ra kết quả mà không ảnh hưởng đến bộ hoặc bộ đóng băng mà chúng được áp dụng. Mặc dù các phần tử của một tập hợp có thể được sửa đổi bất cứ lúc nào, các phần tử của bộ đông lạnh vẫn giữ nguyên sau khi tạo. & NBSP;
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}47
Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}48
Nếu không có tham số nào được thông qua, nó sẽ trả về một frozenset trống. & Nbsp; & nbsp;
Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}4___
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}58
Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}59
Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}02
'n', 'a', 'm', 'e'
3 Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}04
Set with the use of Numbers: {1, 2, 3, 4, 5, 6} Set with the use of Mixed Values {1, 2, 'For', 4, 6, 'Geeks'}0
Các đối tượng đánh máy thành các bộ
'n', 'a', 'm', 'e'
6'n', 'a', 'm', 'e'
7Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}81
Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}82
Đầu ra
Dictionary[key_2].add['name_3']
3Python3
Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}49 3 Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}51 | Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}52 3 4Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}55 |
Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}60 3 27__77744644465Initial blank Set: set[] Set after Addition of Three elements: {8, 9, [6, 7]} Set after Addition of elements from 1-5: {1, 2, 3, [6, 7], 4, 5, 8, 9}2 Set after Addition of elements using Update: {4, 5, [6, 7], 10, 11}9__ | Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}75 3 4Initial blank Set: set[] Set with the use of String: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of an Object: {'e', 'r', 'G', 's', 'F', 'k', 'o'} Set with the use of List: {'Geeks', 'For'}78 |
Đặt phương pháp | Hàm số |
Sự mô tả | cộng[] |
Thêm một phần tử vào một tập hợp | gỡ bỏ[] |
Loại bỏ một phần tử khỏi một tập hợp. Nếu phần tử không có trong tập hợp, hãy nâng một KeyError | xa lạ[] |
Xóa tất cả các phần tử tạo thành một tập hợp | sao chép [] |
Trả về một bản sao nông của một bộ | nhạc pop[] |
Loại bỏ và trả về một phần tử đặt tùy ý. Nâng cao KeyError nếu bộ trống | cập nhật[] |
Cập nhật một bộ với sự kết hợp của chính nó và những người khác | liên hiệp[] |
Trả về sự kết hợp của các bộ trong một bộ mới | Sự khác biệt[] |
Trả về sự khác biệt của hai hoặc nhiều bộ như một bộ mới | arce_update [] |
Xóa tất cả các phần tử của một bộ khác khỏi tập hợp này | loại bỏ [] |
Xóa một phần tử khỏi tập hợp nếu nó là thành viên. [Không làm gì nếu phần tử không được đặt] | ngã tư[] |
Trả về giao điểm của hai bộ dưới dạng bộ mới | Giao lộ_Update [] |
Cập nhật tập hợp với giao điểm của chính nó và một | isdisjoint [] |
Trả về đúng nếu hai bộ có giao điểm null | ISSUBSET [] |
Trả về true nếu một tập khác chứa tập hợp này
phát hành []
- Trả về true nếu bộ này chứa một tập hợp khác
- symmetric_difference []
- Trả về sự khác biệt đối xứng của hai bộ như một bộ mới
- symmetric_difference_update []
- Cập nhật một tập hợp với sự khác biệt đối xứng của chính nó và một bộ khác
- Các bài viết gần đây về bộ Python
- Đặt chương trình
- Chương trình chấp nhận các chuỗi chứa tất cả các nguyên âm
- Chương trình Python để tìm các yếu tố phổ biến trong ba danh sách bằng cách sử dụng các bộ
- Tìm các giá trị thiếu và bổ sung trong hai danh sách
- Chương trình để đếm số nguyên âm bằng cách sử dụng các bộ trong chuỗi đã cho
- Sự khác biệt giữa hai danh sách
- Python đặt để kiểm tra xem chuỗi có phải là panagram không
- Các hoạt động của Python Set [Liên minh, Giao lộ, Sự khác biệt và sự khác biệt đối xứng]
- Chuỗi được nối với các ký tự không phổ biến trong Python
- Từ điển Python, đặt và truy cập để kiểm tra xem tần số có thể trở thành giống nhau không
- Sử dụng set [] trong kiểm tra python pangram
- Đặt bản cập nhật [] trong Python để thực hiện các mảng N
Liên kết hữu ích
- Đầu ra của các chương trình Python - Bộ
- Các bài viết gần đây về bộ Python
- Câu hỏi trắc nghiệm - Python
- Tất cả các bài viết trong danh mục Python