Many of Dict's methods are similar to the list, with one by one of them coming in, and they will be compared to the list.
Nesting
Nesting also exists in the list, where elements are list, and in 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 ' from layer to layer
Get keys, values
In the last lecture, it has been known that you can get its value through the Dict key. Example above.
Is there another way to get the key value? Yes! Python generally does not have a single way to implement an operation.
>>> website = {1: "Google", "second": "Baidu", 3: "Facebook", "Twitter": 4}
>>> #用d. Keys () The method gets all the keys of dict, the result is List
>>> website.keys ()
[1, ' second ', 3, ' Twitter ']
>>> #用d. VALUES () The method gets all the values of dict, if there are no nested other dict, the result is List
>>> website.values ()
[' Google ', ' Baidu ', ' Facebook ', 4]
The method of >>> #用items () 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 results above, we can see that we can also use the For Statement loop to get the corresponding content. For example:
>>> for key in Website.keys ():. Print Key,type (key) ...
1 <type ' int ' >
second <type ' str ' >
3 <type ' int ' >
twitter <type ' str ' >
> >> #下面的方法和上面的方法是一样的
>>> for key in website:
... Print Key,type (key) ...
1 <type ' int ' >
second <type ' str ' >
3 <type ' int ' >
twitter <type ' str ' >
The following two methods are equivalent:
>>> for value in Website.values ():
. . Print value
...
Google
Baidu
Facebook
4
>>> for key in website:
... Print Website[key]
...
Google
Baidu
Facebook
4
The following method is equivalent:
>>> for K,v in Website.items ():
... Print str (k) + ":" +str (v)
...
1:google
second:baidu
3:facebook
twitter:4
>>> for K in website:
... Print str (k) + ":" +str (Website[k])
...
1:google
second:baidu
3:facebook
twitter:4
The following method can also get the key value, but it seems to have to knock more keyboard
>>> 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, as the class is already listed in the previous section, reader if you are interested to try one. Here 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, corresponding to a deep copy.
>>> new_web #两者区别, you can Google it.
{1: ' Google ', ' second ': ' Baidu ', 3: ' Facebook ', ' Twitter ': 4}
There are two ways to delete a key-value pair, but there is a difference
>>> #d. Pop (key), delete the corresponding key value pairs according to key, and return the value
>>> new_web.pop (' second ')
' Baidu '
>> > del new_web[3] #没有返回值, if delete key does not exist, return error
>>> new_web
{1: ' Google ', ' Twitter ': 4}
> >> del new_web[9]
traceback (most recent call last):
File ' <stdin> ', line 1, in <module>
keyerror:9
With D.update (D2), the D2 can be merged into D.
>>> cnweb
{' QQ ': ' In ' the ' in CN ', ' Python ': ' Qiwsir.github.io ', ' Alibaba ': ' Business '}
>>> Website
{1: ' Google ', ' second ': ' Baidu ', 3: ' Facebook ', ' Twitter ': 4}
>>> website.update (cnweb) # Merge Cnweb into website
>>> website #变化了
{' QQ ': ' ' in ', ' 1: ' Google ', ' second ': ' Baidu ', 3: ' Facebo Ok ', ' python ': ' Qiwsir.github.io ', ' Twitter ': 4, ' Alibaba ': ' Business '}
>>> cnweb #not changed
{' QQ ': ' Qiwsir.github.io ', ' python ': ' Business ', ' Alibaba ': '
In the end, to remind reader, in Python3, Dict have a lot of changes, such as to be able to do dictionary parsing, like a list of the resolution, which is very interesting things oh.