Trong hướng dẫn này, chúng tôi sẽ tìm hiểu về phương pháp băm python [] với sự trợ giúp của các ví dụ.
Phương thức
hash[object]4 trả về giá trị băm của một đối tượng nếu nó có một. Các giá trị băm chỉ là các số nguyên được sử dụng để so sánh các khóa từ điển trong một từ điển nhìn nhanh chóng.
Thí dụ
text = 'Python Programming'
# compute the hash value of text
hash_value = hash[text]
print[hash_value]
# Output: -966697084172663693
Hash [] cú pháp
Cú pháp của phương pháp
hash[object]4 là:
hash[object]
Các tham số Hash []
Phương thức
hash[object]4 có một tham số duy nhất:
- Đối tượng - đối tượng có giá trị băm sẽ được trả về [số nguyên, chuỗi, float] - the object whose hash value is to be returned [integer, string, float]
Hash [] giá trị trả về
Phương thức
hash[object]4 trả về giá trị băm của một đối tượng.
Ví dụ 1: Làm thế nào băm [] hoạt động trong Python?
# hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
Đầu ra
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 2230730083538390373
Ví dụ 2: Hash [] cho đối tượng Tuple bất biến?
Phương pháp
hash[object]4 chỉ hoạt động cho các đối tượng bất biến là tuple.
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
Đầu ra
The hash is: -695778075465126279
Ví dụ 2: Hash [] cho đối tượng Tuple bất biến?
Phương pháp
hash[object]4 chỉ hoạt động cho các đối tượng bất biến là tuple.
Làm thế nào để Hash [] hoạt động cho các đối tượng tùy chỉnh?
Như đã nêu ở trên, phương thức
hash[object]4 gọi phương thức nội bộ
# hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
0. Vì vậy, bất kỳ đối tượng nào cũng có thể ghi đè # hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
0 cho các giá trị băm tùy chỉnh.
Dưới đây là các trường hợp ghi đè chính xác 0. | Dưới đây là các trường hợp ghi đè chính xác 0. | Sự mô tả |
Được xác định [theo mặc định] | Nếu còn lại như hiện tại, tất cả các đối tượng so sánh không đồng đều [ngoại trừ chính chúng] | [Nếu có thể thay đổi] được xác định |
Không nên được định nghĩa | Nếu còn lại như hiện tại, tất cả các đối tượng so sánh không đồng đều [ngoại trừ chính chúng] | [Nếu có thể thay đổi] được xác định |
Không nên được định nghĩa | Không nên được định nghĩa | Việc thực hiện bộ sưu tập băm yêu cầu giá trị băm của khóa là bất biến |
Không nên được định nghĩa | Việc thực hiện bộ sưu tập băm yêu cầu giá trị băm của khóa là bất biến | Không xác định |
Không nên được định nghĩa | Việc thực hiện bộ sưu tập băm yêu cầu giá trị băm của khóa là bất biến | Không xác định |
Nếu # hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
3 không được xác định, # hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
0 không nên được xác định.
# hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
# hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
class Person:
def __init__[self, age, name]:
self.age = age
self.name = name
def __eq__[self, other]:
return self.age == other.age and self.name == other.name
def __hash__[self]:
print['The hash is:']
return hash[[self.age, self.name]]
person = Person[23, 'Adam']
print[hash[person]]
Đầu ra
The hash is: 3785419240612877014
Ví dụ 2: Hash [] cho đối tượng Tuple bất biến? You don't have to implement
# hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
3 method for the hash as it is created by default for all objects. Hàm Hash [] là một hàm tích hợp và trả về giá trị băm của một đối tượng nếu nó có một. Giá trị băm là một số nguyên được sử dụng để nhanh chóng so sánh các khóa từ điển trong khi nhìn vào từ điển.is a built-in function and returns the hash value of an object if it has one. The hash value is an integer which is used to quickly compare dictionary keys while looking at a dictionary.
Cú pháp của phương pháp Hash [] của Python:
Cú pháp: Hash [OBJ] hash[obj]
Tham số: & nbsp; obj: đối tượng mà chúng ta cần chuyển đổi thành băm. The object which we need to convert into hash.
Trả về: Trả về giá trị băm nếu có thể. & Nbsp; Returns the hashed value if possible.
Thuộc tính của hàm băm []
- Các đối tượng được băm bằng băm [] là không thể đảo ngược, dẫn đến mất thông tin.
- Hash [] trả về giá trị băm chỉ cho các đối tượng bất biến, do đó có thể được sử dụng như một chỉ số để kiểm tra các đối tượng có thể thay đổi/bất biến.
Phương pháp python băm [] ví dụ
Ví dụ 1: Thể hiện hoạt động của Hash [] & NBSP;Demonstrating working of hash[]
Python3
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903733
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903734
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903735
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903736
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903734
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903738
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903739
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903734
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
1# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
2# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
4 # tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
5 # tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
6# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
8# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
9# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
2# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3The hash is: -6957780754651262792
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
5 # tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
6# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
8The hash is: -6957780754651262797
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
2# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3class Person:
def __init__[self, age, name]:
self.age = age
self.name = name
def __eq__[self, other]:
return self.age == other.age and self.name == other.name
def __hash__[self]:
print['The hash is:']
return hash[[self.age, self.name]]
person = Person[23, 'Adam']
print[hash[person]]
0 # tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
5 # tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
6# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
8class Person:
def __init__[self, age, name]:
self.age = age
self.name = name
def __eq__[self, other]:
return self.age == other.age and self.name == other.name
def __hash__[self]:
print['The hash is:']
return hash[[self.age, self.name]]
person = Person[23, 'Adam']
print[hash[person]]
5Output:
The integer hash value is : 4 The string hash value is : -5570917502994512005 The float hash value is : 1291272085159665688
Ví dụ 2: Thể hiện thuộc tính của Hash [] & NBSP;Demonstrating property of hash[]
Python3
class Person:
def __init__[self, age, name]:
self.age = age
self.name = name
def __eq__[self, other]:
return self.age == other.age and self.name == other.name
def __hash__[self]:
print['The hash is:']
return hash[[self.age, self.name]]
person = Person[23, 'Adam']
print[hash[person]]
6Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903734
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3class Person:
def __init__[self, age, name]:
self.age = age
self.name = name
def __eq__[self, other]:
return self.age == other.age and self.name == other.name
def __hash__[self]:
print['The hash is:']
return hash[[self.age, self.name]]
person = Person[23, 'Adam']
print[hash[person]]
9The hash is: 37854192406128770140
The hash is: 37854192406128770141
The hash is: 37854192406128770140
The hash is: 37854192406128770143
The hash is: 37854192406128770140
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903735
The hash is: 37854192406128770140__77777778
The hash is: 37854192406128770149
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903734
The integer hash value is : 4 The string hash value is : -5570917502994512005 The float hash value is : 12912720851596656881
class Person:
def __init__[self, age, name]:
self.age = age
self.name = name
def __eq__[self, other]:
return self.age == other.age and self.name == other.name
def __hash__[self]:
print['The hash is:']
return hash[[self.age, self.name]]
person = Person[23, 'Adam']
print[hash[person]]
9The hash is: 37854192406128770140
The hash is: 37854192406128770141
The hash is: 37854192406128770140
The hash is: 37854192406128770143
The hash is: 37854192406128770140
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903735______70____777
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
2# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3The tuple hash value is : 83152744337196208104
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
5 # tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
6# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
8The tuple hash value is : 83152744337196208109
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
2# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3hash[object]02
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
5 # tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
6# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
8hash[object]07
Output:
The tuple hash value is : 8315274433719620810
Ngoại lệ: & nbsp;
hash[object]0
Ví dụ 3: băm [] cho đối tượng Tuple bất biến
Python3
hash[object]08
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903734
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3hash[object]11
hash[object]12___
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
2# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
8hash[object]22
Output:
hash[object]1
Ví dụ 4: Hash [] trên đối tượng có thể thay đổithe mutable object
Phương thức băm [] được sử dụng bởi trên đối tượng bất biến, nếu chúng ta sử dụng điều này trên một đối tượng có thể thay đổi như danh sách, đặt, từ điển thì nó sẽ tạo ra một lỗi.
Python3
hash[object]23
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903734
The integer hash value is : 4 The string hash value is : -5570917502994512005 The float hash value is : 12912720851596656881
class Person:
def __init__[self, age, name]:
self.age = age
self.name = name
def __eq__[self, other]:
return self.age == other.age and self.name == other.name
def __hash__[self]:
print['The hash is:']
return hash[[self.age, self.name]]
person = Person[23, 'Adam']
print[hash[person]]
9The hash is: 37854192406128770140
The hash is: 37854192406128770141
The hash is: 37854192406128770140
The hash is: 37854192406128770143
The hash is: 37854192406128770140
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903735
The tuple hash value is : 83152744337196208101
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
2# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
8hash[object]37
Output:
hash[object]2
Ví dụ 5: Hash [] trên một đối tượng tùy chỉnh
Ở đây chúng tôi sẽ ghi đè các phương thức __hash [] __ để gọi băm [] và phương thức __eq __ [] sẽ kiểm tra sự bình đẳng của hai đối tượng tùy chỉnh.
Python3
hash[object]38
hash[object]39
hash[object]40
hash[object]41
hash[object]42
hash[object]43
hash[object]44
hash[object]45
hash[object]46
hash[object]47
hash[object]43
hash[object]49
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903734
hash[object]51
hash[object]47
hash[object]43
hash[object]54
hash[object]45
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903734
hash[object]45
hash[object]40
hash[object]41
hash[object]60
hash[object]43
hash[object]62
hash[object]47
hash[object]64
hash[object]43__
hash[object]40
hash[object]41
hash[object]80
hash[object]43
hash[object]46
hash[object]47
hash[object]64
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
8hash[object]86
hash[object]43__
hash[object]93
Hash for 181 is: 181 Hash for 181.23 is: 530343892119126197 Hash for Python is: 22307300835383903734
hash[object]95
hash[object]96
The hash is: 37854192406128770140
hash[object]98
The hash is: 37854192406128770148
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
2# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3# hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
02 # hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
03 # tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
8# hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
05‘
# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
2# tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
3# hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
02 # hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
03 # tuple of vowels
vowels = ['a', 'e', 'i', 'o', 'u']
print['The hash is:', hash[vowels]]
8# hash for integer unchanged
print['Hash for 181 is:', hash[181]]
# hash for decimal
print['Hash for 181.23 is:',hash[181.23]]
# hash for string
print['Hash for Python is:', hash['Python']]
18Output:
hash[object]3