Dictionary factory functions:
x = {' A ': +, ' B ': $, ' C ': 300}
Dictionary index: #显示没有顺序, unlike lists and functions
Print x[' a ']
100
Dictionary method:
Delete:
X.pop (' a ') #需给出索引参数
Print X
X.popitem () #不需要索引参数, randomly deleted
Delete all elements:
X.clear ()
Print X
Output: {}
Items and Iteritems output dictionary key-value
X.items () X.iteritems ()
Keys and Iterkeys output dictionary middle key
X.keys () X.iterkeys ()
Values and itervalues in the output dictionary
X.values () x.itervalues ()
Get Get Dictionary Key-value (more moderate output)
X.get (' a ')
Output: 100
X.get (' d ')
No output
Has_key confirm if the key is in the dictionary
X.has_key (' a ')
Output: True
X.has_key (' d ')
Output: False
Fromkeys the given key to create a new dictionary
A = Dict.fromkeys ([' Name ', ' Age '], (100))
SetDefault access to the dictionary key-value, if the key is not incremented, the value defaults to none
X.setdefault (' e ', 100)
Update list, add a list of key-values to another list key-value, repeat remove
x = {' A ': +, ' B ': $, ' C ': 300}
z = {' d ': $, ' E ': 6000}
X.update (z)
Print X
Output: {' A ': +, ' C ': +, ' B ': $, ' e ': 6000, ' d ': 500}
A shallow copy of a dictionary: if the key-a value, does not change the value of the original dictionary, if the key-multi-value (list), there will be a change at the same time
z = x.copy ()
Print Z
Output: {' A ': +, ' B ': $, ' C ': 300}
Z[' C '] = 400
Print Z
Output: {' A ': +, ' C ': +, ' B ': 200}
Print X
Output: {' A ': +, ' C ': +, ' B ': 200}
x = {' A ': +, ' C ': +, ' B ': [600, 200]}
y = x.copy ()
Print Y
Output: {' A ': +, ' C ': +, ' B ': [600, 200]}
y[' B '][0] = 300
Print Y
Output: {' A ': +, ' C ': +, ' B ': [300, 200]}
Print X
Output: {' A ': +, ' C ': +, ' B ': [300, 200]}
A deep copy is needed at this time.
From copy import deepcopy
y = x.deepcopy ()
Python automated Operations 1-dictionaries