Creating and using dictionaries
Phone = {' Andy ': ' Max ', ' Bob ': ' 137 ', ' John ': ' 138 '}
A dictionary consists of a key and its corresponding value, which is called an item, in the example, the key is the name and the value is the phone number. Each key is separated from the value by a colon, and the entire dictionary is placed within curly braces. An empty dictionary is denoted by {} with two curly braces.
Function Dict
You can use Dict to create a dictionary from other mappings or key-value pairs.
item = [(' Name ', "Andy"), (' Age ', "]d") = Dict (item) d{' name ': ' Andy ', ' Age ': 12}
You can also use arguments to invoke functions
D = dict (name= ' Andy ', age=12) d{' name ': ' Andy ', ' age ': 12}# Delete dictionary element del d[' name ']d{' age ': 12}# len Returns the dictionary contains the key value logarithm len (d) # Modified value d[' Age ']=20d{' age ': 20}
Dictionary methods
Clear Delete all dictionary entries
d{' age ': ' name ': ' Andy '}d.clear () d{}>>>
Copy returns a new dictionary that has the same key value pair as the original dictionary, and the original is unaffected if the value in the copy is replaced, and the value pointed to by the modified original will be modified
X ={' username ': ' admin ', ' machines ': [' foo ', ' Bar ', ' cc ']}y=x.copy () y[' username ']= ' root ' y{' username ': ' Root ', ' Machines ': [' foo ', ' Bar ', ' cc ']}x{' username ': ' admin ', ' machines ': [' foo ', ' Bar ', ' CC '}
Deepcopy copy both the value and all the values it contains
From copy import deepcopya={}a[' name ']=[' admin ', ' root ']b=a.copy () ab=deepcopy (a) d[' name '].append (' tiger ') a[' name '] . Append (' tiger ') b{' name ': [' admin ', ' root ', ' tiger ']}a{' name ': [' admin ', ' root ', ' tiger ']}ab{' name ': [' admin ', ' root '] }
Get Access Dictionary entry
D ={}print (D.get (' name ')) None dictionary D does not have a key value so return none when using get access, you can also specify a default value of D.get (' name ', ' no ') ' No '
Items returns a list that contains all the dictionary items, but the order in the dictionary is indeterminate
d={' title ': ' Python Web Site ', ' url ': ' http://www.python.org ', ' spam ': 0}d.items () dict_items ([' title ', ' Python web Site '), (' url ', ' http://www.python.org '), (' spam ', 0)])) it = D.items () len (IT) 3 (' spam ', 0) in Ittrue (' name ', ' admin ') in Itfalse
Keys returns a dictionary view that contains the keys in the specified dictionary
D.keys () Dict_keys ([' title ', ' url ', ' spam ')
Pop gets the value associated with the specified key and removes the key value pair from the dictionary
D.pop (' title ') ' Python web Site ' d{' url ': ' http://www.python.org ', ' spam ': 0}
Popitem randomly pops a dictionary entry because the dictionary entry order is indeterminate if the delete dictionary entry is not required to use Popitem
D.popitem () (' spam ', 0) d{' url ': ' http://www.python.org '}
SetDefault gets the value associated with the specified key, and SetDefault adds the specified key-value pair in the dictionary when the dictionary does not contain the specified key
D.setdefault (' name ', ' n ') ' n ' d{' name ': ' n '}d[' name ']= ' admin ' d{' name ': ' Admin '}
Update updates another dictionary
d{' name ': ' admin '}a ={' name ': ' Root ', ' age ': +, ' py ': 3.6}d.update (a) d{' name ': ' Root ', ' age ': ', ' py ': 3.6}a{' name ': ' Root ', ' age ': $, ' py ': 3.6}
Values returns a dictionary view that consists of values in the dictionary, and the views returned by values may contain duplicate values
num= {}num[1]=1num[2]=2num[3]=2num.values () dict_values ([1, 2, 2])
Getting started with Python: dictionaries