Dictionaries are unordered and variable sequences.
The definition dictionary is that the key and value of each element are separated by a colon, and the elements are separated by a comma. All elements are placed in a pair of curly brackets "{}".
The keys in the dictionary can be any immutable data, such as integers, real numbers, complex numbers, strings, tuples, and so on. Key-values cannot have duplicate values.
globals () returns a dictionary containing all global variables and values in the current scope.
locals () returns a dictionary containing all local variables and values in the current scope.
1. Dictionary creation and deletion
Assign a dictionary
>>> dict={'python':'dict','java':'javac'}
>>> dict
{'python': 'dict', 'java': 'javac'}
Create a dictionary using existing data
>>> keys=['a','b','c','d'] >>> values=[1,2,3,4]>>> dictionary=dict(zip(keys,values)) >>> dictionary {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Create a dictionary based on the given key and value
>>> d=dict(name='wang',age='20') >>> d {'name': 'wang', 'age': '20'}
Given content, create an empty dictionary
>>> dict = dict.fromkeys(['name','age','sex']) >>> dict {'name': None, 'age': None, 'sex': None}
Delete the dictionary using del
2. Reading of dictionary elements
Use the key as a subscript to read dictionary elements, or throw an exception if the key does not exist
>>> dict={'name':'wang','age':'20'} >>> dict['name'] 'wang' >>> dict['tel'] Traceback (most recent call last): File "<pyshell#33>", line 1, in <module> dict['tel'] KeyError: ''such
Use the get method of the dictionary object to obtain the value corresponding to the specified key, and can return the specified value when the key does not exist.
>>> dict['score']=dict.get('score',[]) >>> dict['score'].append(98) >>> dict['score'].append(97) >>> dict {'name': 'wang', 'age': '20', 'score': [98, 97]}
Use the items () method of the dictionary object to return a list of dictionary key and value pairs
Use the keys () method of the dictionary object to return the list of keys of the dictionary
Use the dictionary object's values () method to return a list of dictionary values
>>> for item in dict.items(): print(item) ('name', 'wang') ('sex', 'male') ('age', '20') >>> for key in dict SyntaxError: invalid syntax >>> for key in dict: print(key) name sex age
Adding and modifying dictionary elements
When the specified key is a subscript for dictionary assignment, if the key exists, the value of the key can be modified; if it does not exist, it means adding a key, value pair.
>>> dict [ ' age ' ] = 38 #Modify element value >>> dict { ' name ' : ' wang ' , ' sex ' : ' male ' , ' age ' : 38 } >>> dict [ ' adress ' ] = ' china ' #Add new element >>> dict { ' name ' : ' wang ' ,'sex': 'male', 'age': 38, 'adress': 'china'}
Using the dictionary cashing method, the update method adds the key and value pairs of another dictionary to the current dictionary object
>>> dict.update({'a':'a','b':'b'}) >>> dict {'name': 'wang', 'sex': 'male', 'age': 38, 'adress': 'china', 'a': 'a', 'b': 'b'} >>>
3. Adding and modifying dictionary elements
Use del to delete the element of the specified key in the dictionary
Use the clear () method of the dictionary object to delete all elements in the dictionary
Use the pop () of the dictionary object to zoom in and delete and return the element with the specified key
Use the popitem () method of the dictionary object to delete and return an element in the dictionary