A Dictionary of Python

Source: Internet
Author: User

1. Dictionary creation

The dictionary is separated by a colon between each key and its value, and the items are separated by commas, and the entire dictionary is surrounded by a pair of curly braces. An empty dictionary does not include any item {}.

2. Dict function

Like LIST,TUPLE,STR, you can use the Dict function to build a dictionary from other mappings, such as a sequence of other dictionaries or key-value pairs.

Experiment one;

a=[(x,1), (y,2)]

B=dict (a)

b Output {' X ': 1, ' Y ': 2}

You can also create a dictionary with keyword parameters

A=dict (x=1,y=2)

It will also output {' x ': 1, ' Y ': 2}

3. Basic dictionary operation

Len (d) Returns the number of key-value pairs in D

D[K] Returns the value associated to 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 checks if D contains an entry with a key of K

Attention:

The type of the key can make any immutable type, a string or a tuple, not a list

4. Formatting strings for dictionaries

Experiment Two:

a={' x ': 1, ' Y ': 2}

"Zhe shi Shu zi% (x) S"% A is output Zhe shi Shu zi 1.

5. Dictionary Methods

Clear: Clears all items in the dictionary.

Copy: Returns a new dictionary, shallow copy, change new dictionary original dictionary not affected

Fromkeys: Creates a new dictionary with the given key, with each key corresponding to a default value of None

Experiment Three:

{}.fromkeys ([' Name ', ' age ']) output {' name ': None, ' Age ': none}

The example creates an empty dictionary, and then calls the Fromkeys method to create another dictionary, which makes it superfluous to use the Dict.fromkeys ([' Name ', ' age '], ' default '), and default is to set an initial value for it.

Get: Methods for accessing dictionary items

Has_key: Check whether the dictionary contains a specific key, equivalent to K in D, if present, returns True, if not present, returns false.

The items and Iteritems:items methods return all the entries in the dictionary as a list, each item in the list is represented as a key-value pair, the return item has no specific order, and the iteritems is basically the same, but only the iterator is returned.

The keys and Iterkeys:keys methods return a list of keys, and similarly, iterkeys the iterator that returns the key.

Pop: Used to get the value of the given key, and then remove the key value pair from the dictionary.

Popitem: Similar to pop, it handles random items.

SetDefault: Similar to the Get method, returns the value corresponding to the key, if the key exists, returns a value if it does not exist, returns the default setting value, and updates the dictionary.

Update: The dictionary can be updated with another dictionary.

Values and Itervalues: Returns the value in the dictionary, and Itervalues returns an iterator that, unlike the list of returned keys, can contain duplicate elements in the return value list.

This article from "ZYZDBK" blog, declined reprint!

A Dictionary of Python

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.