Storage location =hash (key)
- value pair" storage location, In the structure, take the "key - value pair", according to the same hash is called a hash function . The table constructed by this algorithm is called a hashtable or hash ) .
The hash function establishes a mapping from the key - value pair to the hash table address set, and with the hash function, we can determine the address of the "key - value pair" position in the Hashtable based on the key. Using this method, because there is no need to do multiple key comparisons, so its search speed is very fast, many systems use this method to organize and retrieve data.
For example, there is a set of "key-value pairs":<5, "Tom" >, <8, "Jane" >, <12, "Bit" >, <17, "Lily" >, <20, "Sunny" >, we calculate the key according to the following hash function : hash (x) =x%17+3;Hash (5) =8,hash (8) =11,hash() =15, Hash (=3),hash (=6). We put <5, "Tom">,<8, "Jane">,<12, "Bit">,<17, "Lily">,<20, "Sunny"> is placed at address 8 , 11 , , 3 , 6 The location of the . When you want to retrieve 17 corresponds to the value, as long as the first calculation 17 has a hash value of 3 , then go to the address of 3 where you can get the data to find 17 The corresponding data is "
1.1 Dictionary Creation
>>> dict={' name ': ' Python ', ' age ': 7, ' class ': ' Second '}>>> dict{' name ': ' Python ', ' age ': 7, ' class ': ' Second '}
Tips:1 use {}; 2key-values between: Split 3, use between different items, split
1.2 Dictionary element Access
1) do subscript access via key
>>> dict[' age ']7
2) through method get
>>> dict.get (' sex ') ' Male ' dict.get () >>> dict.get (' name ') does not exist when default estoppel none>>> Dict.get (' name ', ' muyou ') specifies the content returned when not present ' muyou '
2.1 Adding dictionary elements
Unlike the addition of the list, the list cannot be directly assigned if there are no internal variables, must pass, insert,append, and so on, and the dictionary can be assigned directly, the existence is changed, the new object is created and assigned a value.
>>> dict[' sex ']= ' fale ' >>> dict{' name ': ' Python ', ' age ': 7, ' class ': ' Second ', ' Sex ': ' Fale '}
2.2 Deleting a dictionary element
2.2.1 Pop pop-up
Traceback (most recent call last): #不同于list, because of the disorder, you must set the index File "<pyshell#179>", line 1, <module> dict . Pop () typeerror:pop expected at least 1 arguments, got 0>>> dict.pop (' age ') 7>>> dict{' name ': ' Python ', ' Class ': ' Second ', ' Sex ': ' Fale '}
2.2.2 Popitem Popup
>>> Dict.popitem () #如果弹出整个item, you can eject from the back (' sex ', ' fale ')
2.2.3 del Statement deletion
{' name ': ' Python '}>>> del dict[' name ']>>> dict{}>>> del dict>>> dict<class ' Dict ' >
2.2.4 Modifying elements
1) The direct assignment method is shown in the same 2.1
>>> dict[' sex ']= ' make ' >>> dict[' sex ']= ' female ' >>> dict{' sex ': ' Female '}
2) Update modification
>>> dict.update (sex= ' male ') >>> dict{' sex ': ' Male '}
Summary
Beginners learn the dict of Python