Python Learning (11) Python Data type: Dictionary (important)

Source: Internet
Author: User

Dictionary dict:

A dictionary is actually the equivalent of a map in Java to store key-value pairs. The data stored in it is unordered.

If there is such a data:

t1=[' name ', ' age ', ' sex '

t2=[' Tom ', ' Male '

by Zip (t1,t2) you can get to such data [(' Name ', ' Tom '), (' Age ', ' + '), (' Sex ', ' male ')]

The dictionary definition is done through {}.

A dictionary is the only type of mapping (hash table) in Python, where the Field object is variable, but the key of the Dictionary object must be immutable, and a dictionary can contain key values of the type.

Use keys () or values () to return the dictionary's key list or value list.

Items () returns a tuple of key-value pairs.

Dictionary Creation Method:

1. {}

For example d={' name ': ' Tom ', ' age ': +, ' sex ': ' Male '}

2, Factory method Dict ()

D = dict ()
D = dict (name= "Nico", age=23)
D = dict ([' Name ', "Nico"], [' Age ', 23])

3, built-in Method Fromkeys (), this comparison is suitable for the dictionary keys have the same value.

Ddict={}.fromkeys ((' x ', ' Y '),-1)

get {' x ':-1, ' Y ':-1}

Iterate through a dictionary

1d={'name':'Tom',' Age': 29,'Sex':'male'}2 3  forKinchD:4     Printk5 6 It's the same as this print. 7 8  forKinchD.keys ():9     PrintK

                    

How to do the dictionary:

1. New

d[' Tel ']= ' 1234568 '

At this point D is {' name ': ' Tom ', ' age ': ' Sex ': ' Male ', ' tel ': ' 12345678 '}

2. Modification

d[' Tel ']= ' 88888 '

3. Delete

Del (d[' tel ') Delete one of the key values in the dictionary

Del d Delete the entire dictionary

D.pop (' tel ') removes and returns an element with a key value of Tel

D.clear () empties the data in the entire dictionary

4. Take value

d[' name ']

Output Tom

If you take a nonexistent key value and report a Keyerroe exception, Python provides the D.get () method

such as D.get (' address ') is not output, you can also specify the default return value when not: D.get (' Address ', ' ERROR ') output error

Dictionary-related functions:

Type () CMP () str ()

  

http://www.icoolxue.com/play/1924 Nineth Episode

Python Learning (11) Python Data type: Dictionary (important)

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.