Python Basic Tutorial Learning Notes---(4) dictionary

Source: Internet
Author: User
Tags shallow copy

A sequence is a data structure in Python, and mapping is another. Mappings (mapping) refer to values by name. The only mapping structure built in Python is a dictionary. The values in the dictionary are not in a special order, but are stored in a specific key. Keys can be numbers, strings, or even groups of tuples. 1, the use of the dictionary:the sequence is indexed to find the value of the element. the dictionary obtains its value by key. fields are more appropriate in some ways than lists:The ① game board is made up of coordinate values .The ② file is determined using the filename.③ Digital Phone, address a contact personNone of this can be replaced by an index. 2, the creation of the dictionary:A dictionary is made up of key-value pairs, a key-value pair called an item. The key is separated from the value by a colon, and the item is separated from the item by a comma, and the entire dictionary is enclosed by a pair of curly braces. An empty dictionary, that is, a dictionary that does not contain any items, consists of a pair of curly braces. list, with square brackets. tuples, with parentheses. the keys in the dictionary are unique, and duplicates are not allowed in the same dictionary, and the values are not unique. Use the Dict function to create a field. 3. Basic Dictionary operation:the basic behavior of a dictionary includes:①len Returns the number of items in the dictionary. ②dic1[k] Returns the value of key k in dictionary Dic1. ③dic1[k]=v Associates The value V to the key K of the dictionary Dic1. ④del Dic1[k] Delete the key in the dictionary Dic1. ⑤k in Dic1 checks if the dictionary Dic1 contains an entry with a key of K. As you can see, the use of dic1[k]=v can either add new items to the dictionary or change the values of existing items. In addition, there are:① Key type: The key of a dictionary is not necessarily an integer type, it can be a float, a string, a tuple, and so on. ② Auto Add: 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 find whether the key exists, not whether the value exists. 4. Dictionary method:(1) Clear Methodthe Clear method is used to empty the dictionary, but there is no return value. Note the following two different differences:Dicx and Dicy are not two dictionaries, but are two symbols pointing to the same dictionary. Changes to Dicx can affect Dicy. Therefore, re-assigning or emptying the dicx will affect the Dicx dictionary itself, thus affecting dicy. both Dicx and Dicy initially point to the same dictionary. Later, dicx={} was used to re-point dicx to another empty dictionary. Since then, Dicx and Dicy are two different dictionaries, the changes to DICX will not affect the dicy. (2) Copy method:The copy method is primarily used to copy a dictionary into another dictionary, but this copy is a shallow copy (shallow copy). use the Copy method for dictionary x to produce a copy of Y. The addition, deletion, emptying, and substitution of values on Y do not affect X, but changes to the values will affect the X dictionary. (3) deepcopy functionthe Deepcoop function is a deep copy relative to the copy method. But it is not a dictionary built-in method, but a deepcopy function in the copy module. The deepcopy function is a deep copy, and any changes made by non-replicas will not affect the meta-dictionary itself once the copy is complete. (4) Fromeys methodThe Fromkeys method is primarily to create a new dictionary in which all keys are the same value. The new item must give the key, the value can not give, the default is None. As can be seen from the example above, using the Fromkeys method can only produce a new dictionary, not a dictionary itself, or add a new item to an old dictionary. And the Fromkeys method can only use a common value for the added key, that is, all keys are the same value, the key is either none, or it is a value. (5) Get methodThe get method is a method of accessing a dictionary item. If you use a key to access an item in the dictionary, an error occurs when we try to access an item that does not exist in the dictionary. However, using the Get method returns a default value, such as none. This default value can also be changed. Example: Query the user phone and address, the empty user has been processed. (6) Has_key () methodused to check if the dictionary contains a given key. equivalent to K in DiC(7) Items MethodThe items method returns all dictionary entries as a list. Each element of the resulting list is an item (key-value) of the original dictionary, but there is no special order between the item and the item in return. (8) Iteritems methodThe Iteritems method works roughly the same as the items method, but returns an iterator object instead of a list. in many cases the iteritems is more efficient. (9) Keys methodThe keys method is to return all keys in the dictionary as a list. Similar to items. (Iterkeys) methodThe Iterkeys method is to return the keys in the dictionary in the form of iterators. Similar to Iteritems. (one) Popitem methodThe Popitem method is similar to the List.pop in the list. The List.pop method pops up the last element in the list, and popitem pops up a random entry. Because there is no concept of order in the dictionary. This method works well if you want to remove and process items one after the other. each time the Popitem () method is randomly removed from the dictionary. There is no way to append hierarchy in a dictionary, because there is no concept of order in the dictionary. (SetDefault) methodThe Setfault method is somewhat similar to the Get method, which is the ability to get the value associated with a given key. In addition, the Setdefalut method can also set the corresponding key value without the given key in the dictionary. The Setfault method has two functions, one is to view the value of an existing item according to the key, the second is to add a new item for the dictionary, to specify the value in the item when adding, or not to specify, then the default value of None is automatically used.
() Update methodThe update method can use a dictionary to update another dictionary. for an old dictionary that is updated with a new dictionary, the items in the new dictionary can be one or more items. If the old dictionary does not have a key in the new dictionary, then the item in the new dictionary is added to the old dictionary. If the same key is in the old dictionary, the value of the item in the old dictionary is replaced with the value of the item in the new dictionary. Values MethodThe values method is used to return a value in the dictionary as a list. is 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.
(itervalues) methodThe Itervalues method is used to return the values in the dictionary as iterators. Similar to the Iteritems method and the Iterkeys method. Because the values in the dictionary may be the same, the returned list can contain duplicate elements. 5, Summary:       

Python Basic Tutorial Learning Notes---(4) dictionary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.