1. Empty The elements in the dictionary anddict to {}
L.Clear ()-None. Remove all items from L
>>> L ={' Shaw ': $, ' Sam ': $, ' Eric ': 40}
>>> L.clear ()
>>> Print L
{}
2. Returns a shallow copy of a dictionary
L.copy (),a shallow copy of L
>>> L ={' Shaw ': $, ' Sam ': $, ' Eric ': 40}
>>> l.copy ()
{' Shaw ': $, ' Sam ': $, ' Eric ': 40}
3. used to create a new dictionary, the key that is the dictionary of the elements inthe sequence seq ,value is the initial value corresponding to all the keys of the dictionary ( defaults to "None")
Lict. Fromkeys (S[,v]), New lict with the keys from S and values equal to V. V Defaults tonone.
>>> seq = (' Shaw ', ' Sam ', ' Stiven ')
>>> name =dict.fromkeys (seq)
>>> print "Listone: {}". Format (name)
Listone: {' Stiven ': None, ' Shaw ': None, ' Sam ': none}
>>> name =dict.fromkeys (seq,1000)
>>> print "Listone: {}". Format (name)
Listone: {' Stiven ': +, ' Shaw ': +, ' Sam ': 1000}
4 . Returns the value of the specified key if the value does not return a default value in the dictionary (None)
D.get (K[,d]), D[k] if k in D, else D. D Defaultsto None.
>>> L ={' Shaw ': $, ' Sam ': $, ' Eric ': 40}
>>> l.get (' Shaw ')
23
>>> printl.get (' Stiven ')
None
5 . Used to determine if the key exists in the dictionary, or False if the key returns true in the dictionary dict
L.has_key (k)-True if D has a key k,else False
>>> L ={' Shaw ': $, ' Sam ': $, ' Eric ': 40}
>>> L.has_key (' Sam ')
True
>>>l.has_key (' Linux ')
False
6. return a traversed (key , value ) tuple (key-value pair) in a list
L.items (), List of D ' s (key, value) pairs, as 2-tuples
>>> L ={' Shaw ': $, ' Sam ': $, ' Eric ': 40}
>>> L.items ()
[(' Shaw ', '), (' Sam ', ' 40 '), (' Eric ')
7. Returns a dictionary of all keys in a list
L.keys ()a Set-like object providing a view on L ' s keys
>>> L ={' Shaw ': $, ' Sam ': $, ' Eric ': 40}
>>> L.keys ()
[' Shaw ', ' Sam ', ' Eric ']
8. Delete a key-value pair
D.pop (k[,d]), V, remove specified key and return the corresponding value. If key is NotFound, D are returned if given, otherwise keyerror is raised
>>> L ={' Shaw ': $, ' Sam ': $, ' Eric ': 40}
>>> l.pop (' Sam ')
36
>>> L
{' Shaw ': All, ' Eric ': 40}
9. Delete the first key-value pair in the dictionary by default
D.Popitem(k, v), remove and return some (key, value) pair as a 2-tuple; But raise keyerror if D is empty.
>>> L ={' Shaw ': $, ' Sam ': $, ' Eric ': 40}
>>> L.popitem ()
(' Shaw ', 23)
>>> L
{' Sam ': $, ' Eric ': 40}
The setdefault () method is similar to the get () method, and if the key does not already exist in the dictionary, the key is added and the value is set to the default value (if dict If a is already in , It will not be overwritten)
D.SetDefault (k[,d]) ->d.get (k,d), also set D[k]=d if K not in D
>>> L ={' Shaw ': $, ' Sam ': $, ' Eric ': 40}
>>>l.setdefault (' Stiven ')
>>> L
{' Stiven ': None, ' Shaw ': $, ' Sam ': $, ' Eric ': 40}
>>>l.setdefault (' Mira ', 65)
65
>>> L
{' Stiven ': None, ' Mira ': $, ' Shaw ': $, ' Sam ': $, ' Eric ': 40}
>>>l.setdefault (' Shaw ', 18)
23
>>> L
{' Stiven ': None, ' Mira ': $, ' Shaw ': $, ' Sam ': $, ' Eric ': 40}
One by one. Update the key / value pairs of the dictionary dict2 to the dict
L.Update ()
>>> L ={' Shaw ': $, ' Sam ': $, ' Eric ': 40}
>>> A ={' book ': Samsung, ' Apple ': 13}
>>> l.update (A)
>>> L
{' book ': $, ' apple ': $, ' Shaw ': $, ' Sam ': $, ' Eric ': 40}
A . return All values of dic
L.values (...)
>>> L = {' book ': Samsung, ' Apple ': 13}
>>> l.values ()
[45, 13]
This article is from the "Shaw blog" blog, please be sure to keep this source http://opsedu.blog.51cto.com/9265055/1767507
Python Dictionary Method summary