Hướng dẫn how do you create a hash function in python? - làm thế nào để bạn tạo một hàm băm trong python?

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.

__eq__()__hash__()Nhưng để thực hiện đúng băm,
# 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 phải luôn trả về một số nguyên. Và, cả hai phương pháp
# 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 và
# 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 phải được thực hiện.
Dưới đây là các trường hợp ghi đè chính xác
# 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.
Dưới đây là các trường hợp ghi đè chính xác
# 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.
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ĩaNế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ĩaKhông nên được định nghĩaViệ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ĩaViệ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ếnKhông xác định
Không nên được định nghĩaViệ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ếnKhô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)) (adsbygoogle = window.adsbygoogle || []).push({}); # 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.

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: 2230730083538390373 
3
Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 
4
Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 
5

Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 
6
Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 
4
Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 
8

Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 
9
Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 
4
# 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))

3
The hash is: -695778075465126279
2
# 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
The hash is: -695778075465126279
7

# 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
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))
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))

8
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))
5

Output:  

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))
6
Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 
4
# tuple of vowels
vowels = ('a', 'e', 'i', 'o', 'u')

print('The hash is:', hash(vowels))

3
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))
9
The hash is:
3785419240612877014
0
The hash is:
3785419240612877014
1
The hash is:
3785419240612877014
0
The hash is:
3785419240612877014
3
The hash is:
3785419240612877014
0
Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 
5
The hash is:
3785419240612877014
0__77777778

The hash is:
3785419240612877014
9
Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 
4
The integer hash value is : 4
The string hash value is : -5570917502994512005
The float hash value is : 1291272085159665688
1
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))
9
The hash is:
3785419240612877014
0
The hash is:
3785419240612877014
1
The hash is:
3785419240612877014
0
The hash is:
3785419240612877014
3
The hash is:
3785419240612877014
0
Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 
5______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))

3
The tuple hash value is : 8315274433719620810
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
The tuple hash value is : 8315274433719620810
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))

3
hash(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))

8
hash(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: 2230730083538390373 
4
# tuple of vowels
vowels = ('a', 'e', 'i', 'o', 'u')

print('The hash is:', hash(vowels))

3
hash(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))

8
hash(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: 2230730083538390373 
4
The integer hash value is : 4
The string hash value is : -5570917502994512005
The float hash value is : 1291272085159665688
1
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))
9
The hash is:
3785419240612877014
0
The hash is:
3785419240612877014
1
The hash is:
3785419240612877014
0
The hash is:
3785419240612877014
3
The hash is:
3785419240612877014
0
Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 
5
The tuple hash value is : 8315274433719620810
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))

8
hash(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: 2230730083538390373 
4
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: 2230730083538390373 
4
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))

8
hash(object)
86
hash(object)
43__

hash(object)
93
Hash for 181 is: 181
Hash for 181.23 is: 530343892119126197
Hash for Python is: 2230730083538390373 
4
hash(object)
95
hash(object)
96
The hash is:
3785419240612877014
0
hash(object)
98
The hash is:
3785419240612877014
8

# 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'))

18

Output:

hash(object)
3

Chức năng băm trong Python là gì?

hàm băm () trong python được sử dụng để trả về giá trị số nguyên băm của đối tượng chúng ta truyền dưới dạng tham số vào nó IFF đối tượng có thể băm.Nói chung, các giá trị băm được sử dụng để so sánh các khóa từ điển trong khi thực hiện tra cứu từ điển.

Làm thế nào để Python thực hiện băm?

Hàm Hash () chấp nhận một đối tượng và trả về giá trị băm dưới dạng số nguyên.Khi bạn chuyển một đối tượng cho hàm băm (), Python sẽ thực thi phương thức đặc biệt __hash__ của đối tượng.Theo mặc định, __hash__ sử dụng danh tính của đối tượng và __eq__ trả về đúng nếu hai đối tượng giống nhau.. When you pass an object to the hash() function, Python will execute the __hash__ special method of the object. By default, the __hash__ uses the object's identity and the __eq__ returns True if two objects are the same.