Many of the dict methods are similar to the list, with one by one below and a comparison with the list.
Nesting
Nested in the list also exists, that is, the element is a list, in the dict, there are similar styles:
>>> a_list = [[1,2,3],[4,5],[6,7]]>>> a_list[1][1]5>>> a_dict = {1:{"name": "Qiwsir"},2: " Python "," email ":" qiwsir@gmail.com "}>>> a_dict{1: {' name ': ' Qiwsir '}, 2: ' Python ', ' email ': ' qiwsir@gmail.com '}>>> a_dict[1][' name '] #一个嵌套的dict访问其值的方法: Write the key ' Qiwsir ' in one layer
Gets the key, value
In the previous lecture, it was known that the value could be obtained by dict the key. Example above.
Is there any other way to get the key value? Yes! Python is generally not the only one way to implement an operation.
>>> website = {1: "Google", "second": "Baidu", 3: "Facebook", "Twitter":4}>>> #用d. Keys () method to get all the keys of dict The result is list>>> Website.keys () [1, ' second ', 3, ' Twitter ']>>> #用d. Values () method to get all values dict, if there is no nested other dict, The result is list>>> website.values () [' Google ', ' Baidu ', ' Facebook ', 4]>>> #用items () method gets a set of key-value pairs,>> > #结果是list, except that the elements in the list are tuples >>> website.items () [(1, ' Google '), (' second ', ' Baidu '), (3, ' Facebook '), (' Twitter ', 4)]
From the above results, we can see that we can also use the For statement to loop the corresponding content. For example:
>>> for key in Website.keys (): ... Print Key,type (key) ... 1
second
3
Twitter
>>> #下面的方法和上面的方法是一样的 >> > for key in website: ... Print Key,type (key) ... 1
second
3
Twitter
The following two methods are equivalent:
>>> for value in Website.values (): ... Print value ... googlebaidufacebook4>>> for key in website: ... Print Website[key] ... googlebaidufacebook4
The following methods are also equivalent:
>>> for K,v in Website.items (): ... Print str (k) + ":" +str (v) ... 1:googlesecond:baidu3:facebooktwitter:4>>> for K in website: ... Print str (k) + ":" +str (Website[k]) ... 1:googlesecond:baidu3:facebooktwitter:4
The following method can also get the key value, but it seems to hit the keyboard more
>>> website{1: ' Google ', ' second ': ' Baidu ', 3: ' Facebook ', ' Twitter ': 4}>>> website.get (1) ' Google ' >>> website.get ("second") ' Baidu '
Several other common methods
The methods in Dict do not introduce too much here, because the class is listed in the previous section, crossing if interested can try each one. Listed below are a few common
>>> Len (website) 4>>> website{1: ' Google ', ' second ': ' Baidu ', 3: ' Facebook ', ' Twitter ': 4}>>> New_web = Website.copy () #拷贝一份, this copy is also called a shallow copy, which corresponds to a deep copy. >>> new_web #两者区别, can Google a bit. {1: ' Google ', ' second ': ' Baidu ', 3: ' Facebook ', ' Twitter ': 4}
There are two ways to delete a key-value pair, but there is a difference between the two
>>> #d. Pop (key), delete the corresponding key-value pair according to key, and return the value >>> new_web.pop (' second ') ' Baidu ' >>> del New_web[3] #没有返回值, if the DELETE key does not exist, return error >>> new_web{1: ' Google ', ' Twitter ': 4}>>> del new_web[9]traceback (most Recent call last): File "
", line 1, in
keyerror:9
Use D.update (D2) to merge the D2 into D.
>>> cnweb{' QQ ': ' First in cn ', ' Python ': ' Qiwsir.github.io ', ' Alibaba ': ' Business '}>>> website{1: ' Google ', ' second ': ' Baidu ', 3: ' Facebook ', ' Twitter ': 4}>>> website.update (cnweb) #把cnweb合并到website内 > >> website #变化了 {' QQ ': ' First in CN ', 1: ' Google ', ' second ': ' Baidu ', 3: ' Facebook ', ' python ': ' Qiwsir.github.io ', ' Twitter ': 4, ' Alibaba ': ' Business '}>>> cnweb #not changed{' QQ ': ' First in cn ', ' Python ': ' Qiwsir.github. Io ', ' Alibaba ': ' Business '}
At the end of this talk, to remind crossing, in Python3, Dict have a lot of changes, such as the ability to do dictionary parsing, like a list of parsing, this is very interesting thing oh.