Python (8) related dictionaries

Source: Internet
Author: User

The dictionaries in Python are completely different. They are not sequences, they are not ordered, but a ing. The internal elements are separated by commas (,).

. They are stored through keys instead of locations. They are also changeable and can be changed, increased, or decreased locally.

Main attributes:

<1> Read data by key instead of offset

<2> unordered set of any object

<3> variable length, heterogeneous, and arbitrary nesting

<4> variable ing

<5> Object Reference Table

Common Operations

D = {} empty dictionary

'Name' in D member existence Test

D. keys () method: Key

D. values () Value

D. items () Key + Value

D. copy () copy

D. get (key, default) default

D. update (D1) Merge

D. pop (key) Deletion

Len (D) evaluate the length

D [name] = 'ww 'modify and add

Del D [key] Delete

List (D. values () list (D. items () list (D. keys () converts the value to list

Ing operation

>>> D = {}

>>> D ['name'] = 'bob'

>>> D ['job'] = 'dev'

>>> D ['age'] = 40

>>> D

{'Age': 40, 'job': 'dev', 'name': 'bob '}

>>> Print (D ['name'])

Bob

Traversal

Since there is no order, we can first convert the dictionary key to list, sort and print.

>>> D = list (D. keys ())

>>> D

['A', 'C', 'B']

>>> D. sort ()

>>> D

['A', 'B', 'C']

>>> For I in d:

Print (I, '=>', d [I])

A => 1

B => 2

C => 3

Simplified point

>>> For key in sorted (D ):

Print (key, '=>', D [key])

Usage notes:

<1> the sequence operation is invalid (no sequence)

<2> assign a value to the new index to add an item.

<3> keys are not always strings.

<4> avoid missing key errors

Reference < >

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.