Dict dictionary table

A statement: attention to distinguish two different wording statement
1, curly braces {key: value, ...}
, for example, d = { 'ISBN': ' 6775', 'Title': 'python Started', 'price': 39 }
2, with dict (key = value) function
e.g. emp = dict (name = 'tom ', age = 60)
Second, the operation
1, find
(1) in square brackets []
, for example, d [ 'Title'] # if it exists will prompt abnormal
emp [ 'name'] [ ' firstname'] # nested find
(2) d.get # if it exists does not prompt abnormal
d.get ( 'the title')
d.get ( 'title ', 0) in the dictionary lookup table #' title ', and outputs 0 if no
2 add an element
d [' Author '] =' Jerry '# increase of a key in the dictionary table' Author ':' Jerry '
3, the length len dictionary function table ()
4, to change the dictionary table of elements, changing the support place
d [' price '] = 90 # inside the dictionary table corresponding to the key price value varied to 90
5, two dictionaries tABLE combined into a dictionary table d.update ()
. 6, to delete a key .pop ()
d.pop ( 'price') d # inside the dictionary table corresponding to the key price delete
7, only the display key. keys () # result is returned as a list view instead of
8, only the display value .values () # returned as the result rather than a list view
9, keys are displayed .items () # returned as the result rather than a list view
10, a dictionary table nested
e.g. emp = { 'age': 20 , 'name': { 'firstname': 'Jerry', ' LastName ':' Lee '}}
. 11, the key does not support sorting dictionary table, if a certain key sequence is necessary to output, there are two methods
(1) to convert first key extracted from the list and then sort the list
= {e.g. D 'A':. 1, 'B': 2, 'C':. 3, 'D':}. 4
KS = List (d.keys ())
ks.sort ()
(2) using the sorted global function ()
D = { 'A':. 1, 'B': 2, 'C':. 3, 'D':}. 4
KS = d.keys ()
for the sorted in K (KS):
Print (K, D. get (k))
is the output value
a. 1
B 2
C. 3
D. 4

Released eight original articles · won praise 0 · Views 141

Guess you like

Origin blog.csdn.net/DAN_L/article/details/104076366