Eraser, a new series of fun Internet advanced webworms, let’s Be More Pythonic together.

Python hash tables and hashable objects

11.1 Hash Table (Hash Table)

A Hash is a transliteration of a Hash table, also known as a Hash table.

A hash table is an unordered collection of key-value pairs, where each key is unique. The core algorithm is to find a value through an index. Dictionaries in Python conform to the hash table structure, where each key corresponds to a value.

Hashing is the process of using algorithms to map data of any size to a fixed-length output, which is the hash value.

Hash algorithms create high-performance data structures that can quickly store and access large amounts of data, and hash values are computed by hash functions.

A hash function, which is essentially a key-to-value mapping;

A hash table is essentially an array that stores values that have been computed by a hash function;

A hash is a fixed length value that uniquely identifies data.

All of these are conceptual knowledge, which can be understood in the early stage and then gradually mastered with application.

11.2 Hashed and unhashed

The conclusion is that an object (everything in Python is an object) is hashable if it does not change during its lifetime.

An even easier way to prove this is that elements that can be inserted into a set in Python are hashable, as in the following code:

my_set = set()

test = [1.3.14.'hello', (2.3), {'key': 1},1.2] and {3.6}]
my_set.add(test[0])
my_set.add(test[1])
my_set.add(test[2])
my_set.add(test[3])
# my_set.add(test[4])
# my_set.add(test[5])
# my_set.add(test[6])
Copy the code

The results of the tests were as follows:

  • canHashed data structure:int,float,str,tuple;
  • Can not beHashed data structure:dict,list,set.

So, plus what we learned from snowballing, the data types that can be hashed are immutable, and the data types that can’t be hashed are mutable, so it’s a little bit of a roundabout, so just pause a little bit and read it twice.

Hashable objects are commonly used as dictionary keys and collection members because these data structures use hash values internally.

Final conclusion: Hash ≈ immutable.

11.3 Python hash() function

The hash function is used to obtain the hash value of an object. The syntax result is hash(object), and the return value is the hash value of the object. The hash value is an integer. It’s very simple to use:

print(hash('test'))
print(hash(1))
# Notice the following error when using non-hashable objects
# hash ([1, 2, 3])
Copy the code

11.4 hashlib module

Hashlib provides common digest algorithms, as follows:

Md5 (), sha1(), sha224(), sha256(), sha384(), sha512(), blake2b(), blake2s(), sha3_224(), sha3_256(), sha3_384(), sha3_512(), Shake_128 (), shake_256 ()

Use dir(hashlib) to get all of the above available methods.

MD5, the most common digest algorithm, generates a fixed 16-byte result, usually represented by a 32-bit hexadecimal string, as shown in the following code:

import hashlib
# the MD5 algorithm
md5 = hashlib.md5()
data = "hello world"
md5.update(data.encode('utf-8'))
# Compute the hash value and get the encrypted string
print(md5.hexdigest())
Copy the code

The SHA1 algorithm is more secure, and its result is 20 bytes long, usually represented by a 40-bit hexadecimal string. More secure algorithms than SHA1 are SHA256 and SHA512, but the more secure algorithms are slower and have longer abstract length.

11.5 Summary of this blog

This blog has explained Python’s hash table concepts and hashable objects, which are helpful for beginners.

Further down the line, you should try your hand at hashing algorithms and hashable objects by hand.

Blogger ID: Dream eraser, hope you like, comment, favorites.