Một điều quan trọng đã gợi ý về câu trả lời tuyệt vời của Mgilson, nhưng không được đề cập rõ ràng trong bất kỳ câu trả lời hiện có nào:
Số nguyên nhỏ băm cho chính họ:
>>> [hash[x] for x in [1, 2, 3, 88]]
[1, 2, 3, 88]
Chuỗi băm cho các giá trị không thể đoán trước. Trên thực tế, từ 3.3 trở đi, theo mặc định, chúng được xây dựng một hạt giống được chọn ngẫu nhiên khi khởi động. Vì vậy, bạn sẽ nhận được kết quả khác nhau cho mỗi phiên phiên dịch Python mới, nhưng:
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
Vì vậy, hãy xem xét việc thực hiện bảng băm đơn giản nhất có thể: chỉ là một mảng các phần tử N, trong đó chèn một giá trị có nghĩa là đặt nó vào
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
1 [giả sử không có va chạm]. Và bạn có thể đoán ra mức độ lớn của >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
2 sẽ lớn như thế nào, nó sẽ lớn hơn một chút so với số lượng các yếu tố trong đó. Khi tạo một tập hợp từ một chuỗi gồm 6 yếu tố, N có thể dễ dàng, nói, 8.Điều gì xảy ra khi bạn lưu trữ 5 số đó với n = 8? Chà,
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
3, >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
4, v.v. chỉ là những con số, nhưng >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
5 là 0. Vì vậy, mảng của bảng băm cuối cùng giữ >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
6. Vì vậy, thật dễ dàng để tìm ra lý do tại sao việc lặp lại bộ có thể cung cấp cho bạn >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
7.Tất nhiên Python không đảm bảo rằng bạn sẽ nhận được đơn đặt hàng này mỗi lần. Một thay đổi nhỏ đối với cách nó đoán đúng giá trị cho
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
2 có thể có nghĩa là >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
9 kết thúc ở một nơi khác nhau [hoặc kết thúc va chạm với một trong các giá trị khác]. Và trên thực tế, chạy cpython 3.7 trên máy Mac của tôi, tôi nhận được ____ 20.0Trong khi đó, khi bạn xây dựng một hàm băm từ một chuỗi kích thước 11 và sau đó chèn các băm ngẫu nhiên vào nó, điều gì xảy ra? Ngay cả khi giả sử việc thực hiện đơn giản nhất và giả sử không có va chạm, bạn vẫn không biết bạn sẽ nhận được thứ tự nào. Nó sẽ nhất quán trong một lần chạy của trình thông dịch Python, nhưng khác nhau vào lần tới khi bạn bắt đầu nó. [Trừ khi bạn đặt
set1 thành
set2 hoặc cho một số giá trị INT khác.] Đó chính xác là những gì bạn thấy.
Tất nhiên, đáng để xem xét cách các bộ thực sự được thực hiện hơn là đoán. Nhưng những gì bạn đoán dựa trên giả định về việc thực hiện bảng băm đơn giản nhất là [các vụ va chạm và chặn mở rộng bảng băm] chính xác những gì xảy ra.
Một tập hợp là một loại dữ liệu thu thập không có thứ tự có thể sử dụng được, có thể thay đổi và không có các yếu tố trùng lặp. & NBSP;
Đặt được biểu diễn bởi {} [giá trị được đặt trong niềng răng xoăn]
Ưu điểm chính của việc sử dụng một bộ, trái ngược với danh sách, là nó có một phương pháp tối ưu hóa cao để kiểm tra xem một phần tử cụ thể có chứa trong tập hợp hay không. Điều này dựa trên cấu trúc dữ liệu được gọi là bảng băm. Vì các bộ không được đặt hàng, chúng tôi không thể truy cập các mục bằng các chỉ mục như chúng tôi làm trong danh sách.
Ví dụ về các bộ
Python3
set3
set4
set5
set6
set7
set8
set7
set6
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}1
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}3
Output:
set
Phương thức python set [] được sử dụng để đúc loại trong python
Python3
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}4
set4
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}6
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}7
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}8
set7____40
set7
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]22
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]5
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]6
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]7
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]5
Output:
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}
Bộ đóng băng Python
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. Nó có thể được thực hiện với phương thức frozenset [] trong Python. 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. It can be done with frozenset[] method in Python.
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ếu không có tham số nào được truyền, nó sẽ trả về một frozenset trống.
Python
People: {'Idrish', 'Archi', 'Jay'} Set after adding element: {1, 2, 3, 4, 5, 'Idrish', 'Archi', 'Jay', 'Daxit'}1
set4
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}6
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}7
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}8
set7
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]0
People: {'Idrish', 'Archi', 'Jay'} Set after adding element: {1, 2, 3, 4, 5, 'Idrish', 'Archi', 'Jay', 'Daxit'}8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]2
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]3
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}3
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}6
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}7
set4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}9
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}7
Intersection using intersection[] function {3, 4} Intersection using '&' operator {3, 4}1
set7
Intersection using intersection[] function {3, 4} Intersection using '&' operator {3, 4}3
set7
Intersection using intersection[] function {3, 4} Intersection using '&' operator {3, 4}5
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]3
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
Intersection using intersection[] function {3, 4} Intersection using '&' operator {3, 4}9
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Difference of two sets using difference[] function {0, 1, 2} Difference of two sets using '-' operator {0, 1, 2}2
Output:
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]
Làm việc nội bộ của bộ
Điều này dựa trên cấu trúc dữ liệu được gọi là bảng băm. & NBSP; Nếu có nhiều giá trị có mặt ở cùng một vị trí chỉ số, thì giá trị được thêm vào vị trí chỉ mục đó, để tạo thành một danh sách được liên kết. Trong, các bộ Python được triển khai bằng cách sử dụng từ điển với các biến giả, trong đó các sinh vật chính mà các thành viên đặt ra với tối ưu hóa lớn hơn cho độ phức tạp về thời gian.
If Multiple values are present at the same index
position, then the value is appended to that index position, to form a Linked List. In, Python Sets are implemented using dictionary with dummy variables, where key beings the members set with greater optimizations to the time complexity.
Đặt triển khai:
Bộ có nhiều hoạt động trên một hashtable:
Phương pháp cho các bộ
Thêm các phần tử vào bộ python
Chèn vào tập hợp được thực hiện thông qua hàm set.add [], trong đó giá trị bản ghi thích hợp được tạo để lưu trữ trong bảng băm. Giống như kiểm tra một mặt hàng, tức là, O [1] trung bình. Tuy nhiên, trong trường hợp xấu nhất, nó có thể trở thành O [N].O[n].
Python3
Difference of two sets using difference[] function {0, 1, 2} Difference of two sets using '-' operator {0, 1, 2}3
set4
set5
Difference of two sets using difference[] function {0, 1, 2} Difference of two sets using '-' operator {0, 1, 2}6
set7
Difference of two sets using difference[] function {0, 1, 2} Difference of two sets using '-' operator {0, 1, 2}8
set7____90____
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}1
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
Initial set {1, 2, 3, 4, 5, 6} Set after using clear[] function set[]4
Initial set {1, 2, 3, 4, 5, 6} Set after using clear[] function set[]5
set4
Initial set {1, 2, 3, 4, 5, 6} Set after using clear[] function set[]7
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
00>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
01>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
02Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
04 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
05>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
06 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
07__62.>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
14Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
17Initial set {1, 2, 3, 4, 5, 6} Set after using clear[] function set[]5
set4
Initial set {1, 2, 3, 4, 5, 6} Set after using clear[] function set[]7
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
00Output:
People: {'Idrish', 'Archi', 'Jay'} Set after adding element: {1, 2, 3, 4, 5, 'Idrish', 'Archi', 'Jay', 'Daxit'}
Hoạt động của Liên minh trên các bộ Python
Hai bộ có thể được hợp nhất bằng hàm Union [] hoặc | nhà điều hành. Cả hai giá trị bảng băm đều được truy cập và đi qua hoạt động hợp nhất thực hiện trên chúng để kết hợp các yếu tố, đồng thời các bản sao được loại bỏ. Độ phức tạp về thời gian của điều này là O [LEN [S1] + LEN [S2]] trong đó S1 và S2 là hai bộ mà công đoàn cần phải thực hiện. & NBSP;O[len[s1] + len[s2]] where s1 and s2 are two sets whose union needs to be done.
Python3
Difference of two sets using difference[] function {0, 1, 2} Difference of two sets using '-' operator {0, 1, 2}3
set4
set5
Difference of two sets using difference[] function {0, 1, 2} Difference of two sets using '-' operator {0, 1, 2}6
set7
Difference of two sets using difference[] function {0, 1, 2} Difference of two sets using '-' operator {0, 1, 2}8
set7
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
31{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}1
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
33set4
set5
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
36set7
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
38{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}1
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
40set4
set5
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
43set7
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
45{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}1
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
47set4
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
49Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
52Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
55>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
47set4
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
58Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
61Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
55Output:
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}
Hoạt động giao nhau trên các bộ Python
Điều này có thể được thực hiện thông qua giao lộ [] hoặc & toán tử. Các yếu tố phổ biến được chọn. Chúng tương tự như lặp lại trong danh sách băm và kết hợp các giá trị giống nhau trên cả hai bảng. Độ phức tạp về thời gian của điều này là O [Min [Len [S1], Len [S2]] trong đó S1 và S2 là hai bộ mà liên minh cần phải thực hiện.
Python3
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
65set4
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}6
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
68>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
69set4
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}6
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
68>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
04 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
05>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
06 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
07Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2__178
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
12>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
81>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
04 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
05>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
06 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
07__62>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
92>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
93set4
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
95Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
98Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
set01
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
93set4
set04
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
set07
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
set01
Output:
Intersection using intersection[] function {3, 4} Intersection using '&' operator {3, 4}
Tìm kiếm sự khác biệt của các bộ trong Python
Để tìm sự khác biệt ở giữa các bộ. Tương tự để tìm thấy sự khác biệt trong danh sách liên kết. Điều này được thực hiện thông qua sự khác biệt [] hoặc - toán tử. Độ phức tạp về thời gian của việc tìm kiếm sự khác biệt S1 - S2 là O [LEN [S1]]
Python3
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
65set4
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}6
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
68>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
69set4
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}6
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
68>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
04 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
05>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
06 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
07Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2__178
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
12>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
81>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
04 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
05>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
06 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
07__62>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
92>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
92Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
set44
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
set01
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
93set4
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
95Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
set55
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
93set4
set04
Output:
Difference of two sets using difference[] function {0, 1, 2} Difference of two sets using '-' operator {0, 1, 2}
Tìm kiếm sự khác biệt của các bộ trong Python
Để tìm sự khác biệt ở giữa các bộ. Tương tự để tìm thấy sự khác biệt trong danh sách liên kết. Điều này được thực hiện thông qua sự khác biệt [] hoặc - toán tử. Độ phức tạp về thời gian của việc tìm kiếm sự khác biệt S1 - S2 là O [LEN [S1]]
Python3
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
93set4
set41
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
set76
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
set79
set80
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
set83
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
set79
Output:
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
93set4
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
65set51
set52
Normal Set
set[['a', 'c', 'b']]
Frozen Set
frozenset[['e', 'g', 'f']]
4set
01
- Xóa bộ Python
- Đặt phương thức rõ ràng [] làm trống toàn bộ tập hợp.
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
65set
4 set
5>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
09People: {'Idrish', 'Archi', 'Jay'}
Set after adding element: {1, 2, 3, 4, 5, 'Idrish', 'Archi', 'Jay', 'Daxit'}
8set
64People: {'Idrish', 'Archi', 'Jay'}
Set after adding element: {1, 2, 3, 4, 5, 'Idrish', 'Archi', 'Jay', 'Daxit'}
8__
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
Độ phức tạp về thời gian của các bộ | Hoạt động | Trường hợp trung bình | Trường hợp xấu nhất |
ghi chú | x trong s | Trường hợp xấu nhất | Trường hợp xấu nhất |
ghi chú | x trong s | O [1] | Trên] |
& nbsp; | Trường hợp xấu nhất | ghi chú | Trường hợp xấu nhất |
ghi chú | x trong s | Trường hợp xấu nhất | Trường hợp xấu nhất |
ghi chú
x trong s
& nbsp; | Liên minh S | T |
O [Len [S]+Len [T]] | Giao lộ S & T |
O [Min [Len [S], Len [T]] | O [len [s] * len [t]] |
thay thế "min" bằng "tối đa" nếu t không phải là một tập hợp | Nhiều giao điểm S1 & S2 & .. & SN |
[n-1]*o [l] trong đó l là max [len [s1], .., len [sn]] | Sự khác biệt S-T |
O [Len [S]] | Người vận hành cho các bộ |
Bộ và bộ đóng băng hỗ trợ các toán tử sau: | Người vận hành |
Ghi chú | chìa khóa trong s |
Kiểm tra ngăn chặn | Chìa khóa không ở S |
Kiểm tra không liên kết | S1 == S2 |
S1 tương đương với S2 | S1! = S2 |
S1 không tương đương với S2 | S1 |
S1 là tập hợp S2
S1 >> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
65set
4 {'c', 'b', 'a'}
{'d', 'c', 'b', 'a'}
6>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
68
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
69set4
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}6
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
68>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
04 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
05>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
06 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
07__62.>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
81>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
04 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
05>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
06 >>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
07Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2__178
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
12>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
92Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}19
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}20
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}23
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}24
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}27
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
93set4
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}31
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}34
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}35
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}36
set4
set04
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}41
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}42
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}27
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}47
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}48
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}52
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}54
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}47
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}56
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}60
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}54
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}63
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}67
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}47
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}70
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}74
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}27
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}81
set4
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
93set51
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}85
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}88
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}89
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}27
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}47
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}95
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
13Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}99
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]01
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]04
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]8
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]4
Union using union[] function {'Karan', 'Idrish', 'Jay', 'Arjun', 'Archil'} Union using '|' operator {'Deepanshu', 'Idrish', 'Jay', 'Raju', 'Archil'}2
Normal Set set[['a', 'c', 'b']] Frozen Set frozenset[['e', 'g', 'f']]08
{'c', 'b', 'a'} {'d', 'c', 'b', 'a'}89
Output:
>>> [hash[x] for x in 'abcz']
[6014072853767888837,
8680706751544317651,
-7529624133683586553,
-1982255696180680242]
0Các bài viết gần đây về bộ Python.