sorry for the urgent delay yesterday, we went on to speak the dictionary today. First, add three records to the dictionary by key. The square brackets in which is the key, the right side of the equation is value, do not learn the sequence, the total feeling in brackets that is the subscript.
then the dictionary updates, including the increase, deletion, change are also described separately. Delete the whole dictionary is relatively small, after all, the actual work is not like doing exercises, casually deleted can be. So really want to delete the whole dictionary, del Dict1 can. where pop (), this is the equivalent of a stack operation, take out is also equivalent to delete.
Dictionary operation:
1, the Dictionary comparison: CMP (), this comparison is also more complex, after all, is not a complete number and number of size or character than size, inconsistent data types lead to the comparison rules are also troublesome, and use is not many nor much use, interested in their own research. This is a comparison priority (1) Compare dictionary length (2) Compare dictionary keys (3) Compare dictionary values (4) Exact Match
2, Dict (): This is to create a dictionary.
>>> dict (Zip (' x ', ' Y '), (1, 2))
{' Y ': 2, ' X ': 1} The use of the #zip () function is mentioned earlier, there are two tuple parameters in the zip () function seq1 and SEQ2, which can be converted to Seq1[0]seqq2[0],seq1[1]seq2[1],seq1[i]seq2[i] ... , so what if the length of the seq1 is long and the length of the SEQ2 is short? Whichever is shorter, the excess portion of the seq1 is void
>>> dict ([' X ', 1], [' Y ', 2]])
{' Y ': 2, ' X ': 1}
>>> dict ([' XY ' [i-1], i) for I in range (1,3)])
{' Y ': 2, ' X ': 1} #解释一下, followed by A For loop, with the value of I being only 1 and 2, again emphasizing range (start,end), does not produce the end number, only the number between start and end-1. When I is 1 o'clock ' xy ' [0] is ' x ', which is the same as the string through the lower corner of the character, the same ' Y ' corresponds to 2
3, Len (): Seek length. Returns the number of all key-value pairs
4. Keys (), values (), items ()
keys () method, returns a list that contains all the key, the values () method, returns a list that contains all the values in the dictionary, items (), and returns a containing all (key, value) elements a list of groups. These methods are useful when you are not traversing the keys or values of a dictionary in any order.
The keys () method is useful for returning a list that contains all the keys in the dictionary, which can be used with a for loop to gets the value in the dictionary.
Note:
1. One key is not allowed to correspond to multiple values: Just like the update operation, the last operation will overwrite the previous value, right, do you know the concept of function in mathematics, the relationship between X and Y
2. Keys must be hashed, such as mutable types such as lists and dictionaries, because they are not hashed and cannot be used as keys. All immutable types are hashed, so they can all be used as keys to the dictionary. One thing to note is that the problem is a number: numbers with equal values represent the same key. In other words, the hash values for the integer number 1 and the floating-point 1.0 are the same, i.e. they are the same key.
The dictionary as a basic article even if it is finished, of course, until now perhaps you still do not know the charm of the dictionary where, then go to practice it. Practice is the quickest path to progress.
Python Basic Tutorial series: Seven, Dictionary (cont.)