Python dictionaries, collections

Source: Internet
Author: User

A dictionary is a mutable container model and can store any type of object

Characteristics:

The dictionary is unordered, it cannot be accessed by an offset, only by a key.

internal no order, through the key to read the content, can be nested, merge, convenient for us to organize a variety of data structures, and can modify the contents inside, belong to the mutable type.

The keys that make up the dictionary must be immutable data types, such as numbers, strings, tuples, and so on, and mutable objects such as lists cannot be keys

1. Definition of Dictionary

A = {' 1 ': ' A ', ' 2 ': ' B ', ' 3 ': ' C '}
Huo
A = Dict (1= ' A ', 2= ' B ', 3= ' C ')

2. Insert Content

A[4] = ' d '
The result is: {' 1 ': ' A ', ' 2 ': ' B ', ' 3 ': ' C ', ' 4 ': ' d '}

3. Modify the Content

a[' 1 '] = ' q '
The result is: {' 1 ': ' Q ', ' 2 ': ' B ', ' 3 ': ' C ', ' 4 ': ' d '}

4. Delete Content

Del (a[' 1 '])

The result is: {' 2 ': ' B ', ' 3 ': ' C ', ' 4 ': ' d '}

Common methods:

Dict.keys () View dict keys

Divt.values () View all the values in the Dict

Dict.items ()

Dict.clear () Delete all the elements in the dictionary

Dict.pop ()

Dict.get (' key ') gets the value of a key from the dictionary

Dict.has_key (' key ')

Collection

The collection is divided into mutable set (set) and immutable set (Frozenset)

Set hash, Frozenset non-hash (infrequently used)

Set is a non-repeating set of elements that support Union (union), intersection (intersection), difference (differential set), and sysmmetric difference (symmetric difference sets), and other mathematical operations

1. Definition set (SET)

A = set (' Aasdfsdfa ')
The result is: {' A ', ' d ', ' f ', ' s '}

2. Seeking Union (Union)

A = set (' abc ') b = Set (' BCD ') c = a|b
The result is: {' A ', ' B ', ' C ', ' d '}

3. Seeking intersection (intersection)

c = a&b
The result is: {' B ', ' C '}

4, Seek difference (difference set)

c = A-B
The result is: {' A '}

Common methods:

Set.add (obj) adds a value

Set1.difference (Set2) Seeking difference set

Set1.union (Set2) seeking union

Set1.intersection (Set2) to seek the intersection

Set.remove (obj) removing obj

Set.clear ()

Set.pop ()

Python dictionaries, collections

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.