Python basics chapter 4th-dictionary and python basics chapter 4th

Source: Internet
Author: User

Python basics chapter 4th-dictionary and python basics chapter 4th

1. mapping: The data structure that references values by name. The dictionary is the only built-in ing type in Python. The values in the dictionary do not have any special order, but they are all stored in a specific key. Keys can be numbers, strings, or even tuples.

2. typical situations of dictionaries: represents the status of the game board. Each key is a tuple consisting of coordinate values. It stores the number of file modifications and uses the file name as the key. It also provides a digital phone/address book.

3. Create and use dictionaries

Phonebook = {'Alice ': '000000', 'beth': '000000', 'cecil ': '000000'}, using braces, each key and its value are separated by a colon (:), and items are separated by commas (,).

Empty dictionary {}

Dict functions are used to create a dictionary through other mappings (such as other dictionaries) or sequence pairs (such as keys and values ).

Item = [('name', 'gumby'), ('age', 42)]

D = dict (items)

D = {'age': 42, 'name': 'gumby '}

D ['name'] = 'gumby'

4. Basic dictionary operations

Len (d) returns the number of key-value pairs in d.

D [k] returns the value associated with k.

D [k] = v associate value V to k

Del d [k] delete key k

K in d check whether d contains k-key items

Key type: the dictionary key is not necessarily an integer type. automatically added: You can assign a value to the dictionary even if the key does not exist in the dictionary at first. Membership: k in d looks for keys.

5. Dictionary Method

The clear method clears all items in the dictionary. This operation is in-situ and has no return value (None is returned)

Copy method. A new dictionary with the same key-value pairs is returned, which is a light copy.

Deep replication of deepcopy: modifies the original dictionary and does not modify the dictionary subsequently copied.

Fromkeys creates a new dictionary using the given key. Each default value is None.

The get method is a more loose Method for accessing dictionary items. Get can access items that do not exist in the dictionary and return None.

Has_key: Check whether the dictionary contains the given key. D. has_key (k) is equivalent to k in d

Items returns all dictionary items in the list (Key, value)

Iteritems acts on the same items, but returns an iterator object instead of a list. Iteritems is more efficient

Keys returns the dictionary key in the form of a list

Iterkeys returns the iterator for the key

The pop method is used to obtain the corresponding value and then remove the key-value pair from the dictionary.

The popitem method is similar to list. pop. The latter will pop up the last element of the list. However, the difference is that popitem pops up random items, because the dictionary does not have the concept of "last element" or other sequence.

To some extent, the setdefault method is similar to the get method, that is, it can obtain the value associated with the given key. In addition, setdefault can also set the corresponding key value if the dictionary does not contain a given key. If the key does not exist, setdefault returns the default value and updates the dictionary accordingly. If the key exists, the corresponding value is returned without changing the dictionary. The default value is optional. If this parameter is not set, the default value is None.

The update method can use one dictionary item to update another dictionary. Items in the provided dictionary will be added to the old dictionary, and will be overwritten if the same key exists.

Values and itervalues return the values in the dictionary in the form of a list, and the latter return the value iterator. Unlike the return key list, the Return key list can contain duplicate elements.

  

 

  

 

  

 

  

 

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.