dictionary (dict): The combination of unordered data that exists as a key-value pair is a dictionary, why do you say that, we all know that we have learned that the series such as lists, sets, etc. are only a single data can be stored, if you want to store the relevant data is more troublesome, For example, we want a person's name and his gender corresponding to the storage, this time using the list and so on more trouble, then we draw the concept of the dictionary,
definition : A combination of unordered data that exists as a key-value pair is a dictionary,
features : The dictionary stores data is stored in key-value pairs, so-called key-value pairs (key&values) is a key corresponding to a value, then the associated data can be stored
format : dicts = {' A ': ' A '} #注意此时数据是以冒号连接的, the identity symbol is curly brace {}.
simple creation of dictionaries :
1. Create an empty dictionary
Dicts = {} or vardict = Dict ()
2. Create a dictionary with multiple data
2.1 dicts = {' A ': ' A ', ' B ': ' B ', ' C ': ' C '}
2.2 Dicts = Dict ({' A ': ' A ', ' B ': ' B ', ' C ': ' C '})
2.3 dicts = dict (a = ' a ', B = ' B ', c = ' C ')
2.4 lists = [
[' A ', ' a '],
[' B ', ' B ']
]
res = dict (lists)-to-print (res) #注意, only this structure can be converted to a list-to-dictionary
2.5 keys = [' A ', ' B ', ' C ']
values = [' A ', ' B ', ' C ']
res = dict (Zip (keys,values))
Print (res) #这种格式也可以得到字典
Basic operation of the dictionary:
Create a dictionary using: vardict = {' A ': ' A ', ' B ': ' B ', ' C ': ' C ', ' d ': ' d ', ' e ': ' E ', ' F ': ' F '}
1. Accessing values in the dictionary
Print (vardict [' B ']) #访问时书写字典的key, you can get the value of the dictionary
2. Modify the Dictionary
Vardict[' e '] = EE #此时操作就会将原有的e对应的E改成EE
Vardict[' g '] = g #在字典中增加一个键值对 g->g
3. Delete a dictionary
Del vardict [' C '] #次操作删除了原有字典c所对应的键值对
4. Member Detection
Print (' C ' in vardict) #检测成员c是否在字典vardict中 so that only the key can be detected
The traversal operation of the dictionary
Method One: #先遍历键, in the value
For I in Vardict:
Print (i, vardict [i])
Method Two: Simultaneously traverse the key and the value
For x, y in Vardict.items ():
Print (x, y)
Dictionary derivation type
Create the dictionary you use
Vardict = {' A ': ' A ', ' B ': ' B ', ' C ': ' C '}
Vardict2 = {' name ': ' Old king ', ' age ': ' + ', ' sex ': ' Man '}
1. General Deduction Formula
Print ({' * ' + k + ' * ' : ' * ' + k + ' * ' for k,v in Vardict.items ()})
2. Expressions with judging criteria
Print ( k:v for k,v in Vardict2.items () If Len (2) = = 2})
3. Multi-loop Dictionary expressions
Print ({a+x:b+y for a, b in Vardict.items () for x, y in Vardict2.items ()})
Related functions of the dictionary
Create the dictionary you use
Vardict = {' A ': ' A ', ' B ': ' B ', ' C ': ' C '}
1.clear () Empty dictionary
Vardict.clear () #此操作会清空字典的所有内容, please note
2. Copy () Duplicate dictionary
New_vardict = Vardict.copy ()
3. Fromkeys () use a sequence (as a dictionary key) and a specified value to make a dictionary
Lists = [1,2,3,4,5,6]
New_vardict = Dict.fromkeys (lists, ' Python ') or new_vardict = '. Fromkeys (lists, ' Python ')
4.get () Gets the value of the specified key in the dictionary
Vardict.get (' A ', ' return value ') #等价于 var[' a '] #注意get有返回值, the query does not return the returned value after
5.setdefault () Add an element to the dictionary (nonexistent key added, existing key is not moved)
Vardict.setdefult (' F ', ' F ')
6.update () modifying elements in a dictionary
Vardict.update (a = ' a ') or var.update ({' A ': ' A '})
7.pop () removes the specified element from the dictionary
Vardict.pop (' B ')
8.popitem () randomly deletes an element from the dictionary
Vardict.popitem ()
9.keys () Gets a container of all the keys in the dictionary
Print (Var.keys ())
10.values () Gets a container of all the keys in the dictionary
Print (Var.values ())
11.items () Gets a container of all the keys and values in the dictionary
Print (Vardict.items ())
#注: If you have errors or questions, email: [Email protected]
Python Learning notes--dictionaries