Basic Python Tutorial (chapter fourth)

Source: Internet
Author: User

The Dict () function can be created by other mappings or by a sequence (key, value) such as a dictionary//dict is not a function, it is the same type as List tuple str

Items = [(' Name ', ' Gumy '), (' Age ', 20)]

D = dict (items)

or D = dict (name = ' Gumy ', age = 20)

Basic Dictionary Operations

Len (d) The number of key-value pairs in translation dictionary D

D[K] Returns the value on the key K

D[k] = V associates the value V to the key K

Del D[k] Delete item with key K

K in D check if there is a key k in D

formatting strings for Dictionaries

Phonebook = {' Beth ': ' 9102 ', ' Alice ': ' 2341 ', ' Cecil ': ' 3253 '}

"Alice ' s phone is% (Alice) S"% phonebook

The method of the dictionary

Clear () Clears all items in the dictionary

Copy () Shallow copy

Deepcopy () deep copy

Fromkeys () using the given key CV dictionary, each key default value bit None

{}.fromkeys ([' Name ', ' age ']) is equivalent to Dict.fromkeys ([' Name ', ' age '])

Has_key detect if a given key is included in the dictionary

Items returns all dictionary items in a list, with no special order

Iteritems is similar to items except that it returns an iterator, which is then listed using the list method:

it = D.iteritems ()

List (IT)

The Keys method returns the key as a dictionary, while Iterkeys returns an iterator for the key

Pop deletes key value pairs based on key value

D = {' X ': 1, ' Y ': 2}

D.pop (' x ')

The Popitem method, similar to pop, uses this method if it is deleted in order of one after another, and the method does not have to enter a key value

The Get method is similar to the SetDefault method where the given key value obtains the value associated with the key, and for the first time, the SetDefault can also set the corresponding key value without the given key in the dictionary.

Update will be added to the old dictionary based on the entries in the supplied dictionary, and will overwrite if the same key is available.

The values method returns the value in the dictionary as a list, and Itervalues returns a worthwhile iterator.

Basic Python Tutorial (chapter fourth)

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.