Basic python Tutorial Study Notes --- (4) dictionary and python Study Notes
Sequence is a data structure in python, and ing is another. Mapping refers to a value by name. The only ing structure built in python is a dictionary. The values in the dictionary have no special order, but are stored in a specific key. Keys can be numbers, strings, or even tuples.1. Usage of dictionaries:The sequence uses indexes to locate the value of an element. The dictionary uses a key to obtain its value. Fields are more suitable than lists in some aspects: ① the game board is composed of coordinate values. ② the file is determined by the file name. ③ the digital phone number, the address corresponding to a contact cannot be replaced by an index.2. Create a dictionary:A dictionary is composed of key-value pairs. A key-value pair is called an item. Keys and values are separated by colons, and items are separated by commas. The entire dictionary is enclosed by a pair of braces. An empty dictionary, that is, a dictionary that does not contain any item, is composed of a pair of braces. List, in square brackets. Tuples in parentheses. Keys in the dictionary are unique. Duplicate keys and values are not allowed in the same dictionary. Use the dict function to create a field.3. Basic dictionary operations:The basic actions of the dictionary include: ① len returns the number of items in the dictionary. ② Dic1 [k] Return the value of k in the dictionary dic1. ③ Dic1 [k] = v associates the value v with the key k of the dictionary dic1. ④ Del dic1 [k] Delete the key k in the dictionary dic1. ⑤ K in dic1 check whether the dictionary dic1 contains k keys. As you can see, dic1 [k] = v can be used to add new items to the dictionary or change the values of existing items. In addition, there are: ① key type: the dictionary key is not necessarily an integer type, it can also be a floating point type, String, tuples, and so on. ② Automatic addition: When a value is assigned to a key that does not exist in the dictionary, such a key-value pair is automatically created in the dictionary. ③ Membership: k in dic1 is used to check whether the key exists, rather than whether the value exists.4. dictionary method:(1) the clear method is used to clear the dictionary, but no return value is returned. Note the following two differences: dicx and dicy are not two dictionaries, but two codents pointing to the same dictionary. A change to dicx affects dicy. Therefore, the re-assignment or clearing of dicx affects the dicx dictionary and dicy. Both dicx and dicy initially point to the same dictionary. Later, we used dicx ={} to point dicx to another empty dictionary. After that, dicx and dicy are two different dictionaries. Changes to dicx do not affect dicy. (2) copy method: the copy method is mainly used to copy a dictionary to generate another dictionary, but this copy is a shallow copy ). Use the copy method to generate a copy y for dictionary x. Adding, deleting, clearing, and replacing items in y do not affect x, but changing the value affects the x dictionary. (3) The deepcopy function deepcoop is a deep copy relative to the copy method. But it is not a built-in dictionary method, but the deepcopy function in the copy module. The deepcopy function is a deep copy function. After the copy is complete, any changes made to the non-copy will not affect the meta-dictionary itself. (4) The fromeys method fromkeys is mainly used to create a new dictionary. All keys of the dictionary are the same value. A key must be provided for a new project. If the value is left blank, the default value is None. From the above example, we can see that the fromkeys method can only generate a new dictionary, instead of changing the dictionary itself, or adding new items to an old dictionary. In addition, the fromkeys method can only use one common value for the added key, that is, the values of all keys are the same, and the keys are either none or a value. (5) The get method is a method for accessing dictionary items. If keys are used to access the items in the dictionary, an error occurs when we try to access the items that do not exist in the dictionary. However, the get method returns a default value, such as none. This default value can also be changed. For example, the user's phone number and address are queried, and the blank user is processed. (6) The has_key () method is used to check whether the dictionary contains the given key. It is equivalent to the k in dic (7) items method. The items method returns all dictionary items in list mode. Each element in the generated list is an original dictionary (key-value), but there is no special order between items returned. (8) The iteritems method iteritems functions roughly the same as the items method, but returns an iterator object instead of a list. Iteritems is more efficient in many cases. (9) The keys method keys returns all keys in the dictionary as a list. Similar to items. (10) The iterkeys method iterkeys returns keys in the dictionary as iterators. Similar to iteritems. (11) The popitem method is similar to list. pop in the list. The list. pop method will pop up the last element in the list, and popitem will pop up a random item. Because there is no concept of order in the dictionary. This method is very effective if you want to remove and process items one by one. Each time the popitem () method is used, an item is randomly removed from the dictionary. There is no append level method in the dictionary, because there is no sequence concept in the dictionary. (12) The setdefault method setfault method is similar to the get method to some extent, that is, it can obtain the value associated with the given key. In addition, the setdefalut method can also set the corresponding key value without a given key in the dictionary. The setfault method has two functions: one is to view the value of an existing item based on the key, and the other is to add a new item for the dictionary. You can specify the value in the item when adding the item, if this parameter is not specified, the default value None is automatically used.
(13) the update method can be used to update another dictionary. For an old dictionary, use the new dictionary to update it. The items in the new dictionary can be one or multiple. If there is no key in the new dictionary, the key in the new dictionary will be added to the old dictionary. If the old dictionary has the same key, the value of the item in the new dictionary replaces the value of the item in the old dictionary. (14) The values method values is used to return the value in the dictionary in the form of a list. Similar to the items method and the keys method. Because the values in the dictionary may be the same, the returned list can contain duplicate elements.
(15) The itervalues method itervalues is used to return the value in the dictionary as an iterator. The iteritems and iterkeys methods are similar. Because the values in the dictionary may be the same, the returned list can contain duplicate elements.5. Summary: