Guide to the development of PYTHON3 programs
Mappings: Combination of key-value data items
Python3 supports two unordered mapping types: The Built-in Dict type, the collections.defaultdict type in the standard library.
An ordered mapping type is introduced in Python3.1: Collections.ordereddict, which has the same methods and properties as the built-in dict, but is inserted in the order in which the data is stored.
Key: Object reference of the object that can be hashed; Value: Object reference for any type of object
Created with dictionary literals >>> d1=dict ({"id": 1948, "name": "Washer", "Size": 3}) >>> d1{' id ': 1948, ' name ': ' Washer ', ' Size ': 3} Use keyword arguments to create >>> d2=dict (id=1948,name= "washer", size=3) from the sequence, and the zip () function in D4 returns a tuple list >>> d3= Dict (["id", 1948), ("name", "washer"), ("Size", 3)]) >>> d4=dict (("id", "name", "Dize"), (1948, "Washer", 3) ) created from dictionary literals >>> d5={"id": 1948, "name": "Washer", "Size": 3}
Iteration:
>>> for item in D1.items ():p rint item[0],item[1]id 1948name washersize 3 The second method >>> for Key,value in D1.ite MS ():p rint Key,valueid 1948name washersize 3
Iterate based on the value of a dictionary
>>> for key in D1.keys ():p rint key>>> for key in D1:print key>>> for value in D1.values ():p rint Value
Dictionary methods
| D.clear () |
|
| D.copy () |
Shallow copy |
| D.fromkeys (S,V) |
Returns a dict that is the key of the sequence s, with a value of None or the value in V (if parameter V is given) |
| D.get (k) |
Returns the value corresponding to the key K, if K does not exist, returns none |
| D.get (K,V) |
Returns the value corresponding to the key K, if K does not exist, returns V |
| D.items () |
Returns the (Key,value) pair in D |
| D.keys () |
|
| D.pop (k) |
Returns the value of K corresponding to the key K, if no Keyerror exception is generated |
| D.pop (K,V) |
Returns the value of K corresponding to the key k, and if not, returns the V |
| D.popitem () |
Returns and removes an arbitrary (key,value) in dict if D is null to produce a KEYERROR exception |
| D.setdefault (K,V) |
With D.get ()
Difference: If K is not in D, insert a key of K, the value is None or V (if the parameter V is given) |
| D.update (a) |
A (Key,value) not contained in D is added to D
At the same time the key in A and D, replace value in D with value in a
A: Can be a dictionary, can make (key,value) pair of a iterable, or keyword parameter |
| D.values () |
Returns all values in D |
Python mapping Type--notes