python algorithm - hash table

First, the definition of
the hash table (Hash table, also called a hash table), based on the key code value (Key and value) to directly access a data structure. In other words, to access the records by key values are mapped to table a position to speed up the search. This mapping function called a hash function, recording storage array is called a hash table.

M given table, there exist a function f (key), for any given key value key, if obtained after substituting the function containing the address recorded in the keyword table, M is called a hash table (the Hash) table, the function f (key) is hash (the hash) function.

Individual understood: the list is different from the index of the hash table is a Key, Key value directly accessible value, which is different from the list, by an integer [0, ∞) [0, \ infty) [0, ∞) is stored as an index value.

insert (key, value) insert key-value pairs
to get the value GET (Key)
the Delete (Key) Delete value

Hash Table Application :
① collection of dictionaries and
dictionary and a set of hash tables are implemented
using a hash table to store the dictionary, the dictionary by a hash function built mapped to index
if the hash collision occurs, the law or by a zipper development addressing method to solve
② MD5 algorithm (SHA2 algorithm)
md5 was once commonly used in cryptography
use md5 to verify file download is complete
cloud storage service providers use to determine whether he has been on the cloud server, do not re-upload

Second, the conflict
may get different keywords the same hash address, k1 ≠ k2, and f (k1) = f (k2 ), a phenomenon known as conflict (English: Collision). Keyword has the same function value of the hash function is known as a synonym. As described above, according to the method of the hash function f (k) and a conflict will be mapped to a finite set of keywords contiguous address set (interval), and concentrated to address keywords "like" as the recording storage location in the table, this table will be called a hash table, making this mapping process is called a hash or hash table, the resulting storage location called a hash address.

Three, Python application
3.1 dictionary
in Python dictionary will apply the principles of the hash table, make keys and values correspond.

Initialization dictionary:

a = dict()
b = {}
c = {'a': 1, 'b': 2, 'b': '3'}  # 冒号左边是key,右边是value,由于存在重复的b,最后剩下右边的'b':3

Access the dictionary value:

print "c['a']: ", c['a']

Modifying a dictionary

c['a'] = 0 # 将原来映射的1变为0
c['c'] = 4 # 添加新的键值对

Delete dictionary elements

del c['a'] #删除key为a的键值对
c.clear #清除字典c中所有的键值对
del c #直接删除字典c

Dictionary built-in functions and methods

The method of action or function
cmp (dict1, dict2) compare two dictionary elements
len (dict) dictionary length
str (dict) a printable string representation of the dictionary output
type type (variable) output variables
dict.clear () to delete all the dictionaries element
dict.copy () returns a copy of dictionary
dict.fromkeys (seq [, val]) creates a new dictionary, the sequence elements do seq dictionary key, val corresponding to all the keys to the dictionary initial value
dict.get (key , default = None) returns the value of the specified key, if the value is not in the dictionary returns default value
dict.has_key (key) returns true if the key is in the dictionary dict, otherwise return false
dict.items () returns a list traversal (key , value) tuple array
dict.keys () returns a list of all the keys of a dictionary
dict.setdefault (key, default = None) and get () Similarly, if the key does not exist in the dictionary, and the key will be added value is set to default
dict.update (dict2) dict2 the dictionary of key / value pairs to the dict update in
dict.values () to return a list of all the values in the dictionary
pop (key [, default]) to delete the dictionary given key key corresponding to a value, the return value is deleted. key value must be given. Otherwise, return default values.
popitem () returns a random and remove the pair of keys and values in the dictionary.

Guess you like

Origin blog.csdn.net/xili2532/article/details/91412097