Dictionary
The function of the data structure of a dictionary is the same as its name, and the value is obtained by key.
-
Method:
dict={' key1 ': value1, ' key2 ': value2 ...}
for dictionaries, the key value cannot be duplicated in the dictionary, and if duplicates occur, the value of the original key will be changed.
-
basic operation of the dictionary
Use dictionary: dict={' Key1 ': 1, ' Key2 ': 2, ' Key3 ': 3, ' Key4 ': 4}
(1). Modify dictionary
Method: Dict[key]=new_value
>>>dict[' Key3 ']= ' jdieiq ' >>>dict[' Key3 '] ' Jdieiq '
(2). Delete a dictionary element
Method 1:del dict[' key ']
If there is no key, delete the entire dictionary, the method and Dict.clear () The difference is del delete the entire dictionary, the dictionary does not exist, and clear method knowledge Empty dictionary, the Dictionary object is present.
>>>del dict[' Key3 ']>>>dict{' key1 ': 1, ' Key2 ': 2, ' Key4 ': 4}
Method 2:dict.pop (' key ')
This method pops up a key value pair corresponding to the value of the key, and if key is not, the last key value pair in the popup dictionary.
>>>dict.pop (' Key2 ') 2>>>dict{' key1 ': 1, ' Key4 ': 4}
(3). The length of the dictionary
Method: Len (dict)
The Len () function is used for the method of length, whether it is a list, a tuple, or a dictionary.
-
Use dictionary: dict={' Key1 ': 1, ' Key2 ': 2, ' Key3 ': 3, ' Key4 ': 4
(1). Clear () method
Function: Empty dictionary, no return value
(2). Copy () method
function: Shallow copy a dictionary
Shallow copy: Refers to the copied dictionary value changes after copying, does not affect the value of the original dictionary
>>>dict1=dict.copy () >>>dict1{' Key4 ': 4, ' Key3 ': 3, ' key2 ': 2, ' key1 ': 1}>>>dict1[' Key3 ']= ' fjiew ' >>>dict1{' key4 ': 4, ' Key3 ': ' Fjiew ', ' key2 ': 2, ' key1 ': 1}>>>dict{' key1 ': 1, ' key2 ': 2, ' Key3 ': 3, ' Key4 ': 4}
(3). Fromkeys (Seq[,value]) method ,
Function: Returns a dictionary with SEQ as the key value, if value does not exist, the value of the dictionary created is none, If value exists, the value that corresponds to the dictionary key value created is value.
(4). Get (key)
Function: Returns the value of the key value, or null if key does not exist, unlike dict[ Key] method, Dict[key] method, returns an error if key does not exist.
>>>dict.get (' Key3 ') 3>>>dict.get (' key8 ') >>>dict[' Key8 ']traceback <most recent call last:> File "<stdin>",line 1,in <module>keyerror: ' Key8 '
(5). SetDefault (Key,default=none) method
Function: This function is the same as get, but is not the same as when the key value does not exist, save the key in the dictionary and assign the value to None
>>>dict.setdefault (' Key8 ', default=none) >>>dict{' key1 ': 1, ' Key2 ': 2, ' Key3 ': 3, ' Key4 ': 4, ' Key8 ': None}
(6). Items () method
Function: Returns as a list of tuples that can traverse a key value as an element, a key-value pair as a tuple, and a list of left and right tuples to form
>>>dict.items (Dict_items (' Key1 ', 1), (' Key2 ', 2), (' Key3 ', 3), (' Key4 ', 4), (' Key8 ', None)])
(7). Keys () method and values () method
Function: Returns the key list and the value list, respectively
>>>dict.keys () Dict_keys ([' Key1 ', ' key2 ', ' Key3 ', ' key4 ', ' key8 ']) >>>dict.values () dict_values ([ 1,2,3,4,none])
(8). Update (New_dict) method
Role: To bring a new dict_new to the Dict
>>>dict1={' 1 ': 2, ' 3 ': 4}>>>dict.update (dict1) >>>dict{' key1 ': 1, ' key2 ': 2, ' Key3 ': 3, ' key4 ': 4, ' 1 ': 2, ' 3 ': 4}
-
The difference between a dictionary and a list
Features of Dict:
Find and insert very fast and do not increase as key increases
Takes up a lot of memory and consumes more memory
Features of list:
Find and insert times increase as the element increases
Small footprint, small waste of memory
[Python] Learning Basics: Dictionaries