Python Basics--Dict&set

Source: Internet
Author: User
Tags set set

1. Dictionary Dict 1.1 Dictionary Definitions

>>> d={' Adam ': Up, ' Lisa ': $, ' Bart ': 59}

1.2 Dictionary Access

1.2.1key Method Access

Dict[key]

>>> d[' Adam ']

95

Determine if key is--in in Dict(can only Judge key)

>>> D

{' Lisa ': $, ' Adam ': Up, ' Bart ': 59}

>>> ' Paul ' in D

False

>>> ' Lisa ' in D

True

>>> in D

False

1.2.2get Method Access

Dict.get (Key)

The dict itself provides a GET method that returns none when key does not exist

>>> d[' Paul '

Traceback (most recent):

File "<stdin>", line 1, in <module>

Keyerror: ' Paul '

>>> d.get (' Adam ')

95

>>> d.get (' Paul ')

1.3 Dictionary features

Find Fast, high memory consumption

Key value is not repeatable

Key-value to No order

The object type as key must be an immutable type

Immutable types: string, floating-point, integer, etc.

Variable type: List etc

1.4 Dictionary Updates

Dict[key]=value

Note: if the key already exists, the assignment replaces the original value with the new value

1.5 Dictionary Traversal1.5.1 Iteration Dict's keys

When the for-in statement acts directly on the dict, it gets the Dict key.

>>> for key in D:

... print "%s:%s"% (Key,d[key])

...

Lisa:85

Adam:95

bart:59

Values of 1.5.2 iterative Dict

Method

Dict.values () takes all values of the dict out and converts it to a list

Dict.itervalues () simply takes out all values of dict and does not convert to list, so it consumes less memory than the Dict.values () method

Example

>>> d.values ()

[85, 95, 59]

>>> d.itervalues ()

<dictionary-valueiterator Object at 0x0000000002487f98>

>>> for I in D.itervalues ():

... print I

...

85

95

59

Keys and values of 1.5.3 iteration Dict

Method

Dict.items () converts the Dict object to a list containing a tuple

Dict.iteritems () does not convert the dict into a list, but instead continuously gives the tupleduring the iteration, so iteritems () does not occupy additional memory .

Example

>>> D.items ()

[(' Lisa ', "), (' Adam ',"), (' Bart ', 59)]

>>> D.iteritems ()

<dictionary-itemiterator Object at 0x0000000002487f98>

>>> for Key,value in D.iteritems ():

... print Key,value

...

Lisa 85

Adam 95

Bart 59

2. Set Set 2.1 features

The elements of set are not duplicated

The set stored element is similar to the Dict key and must be an immutable object

The elements of set are unordered

2.2 Create

Call set () and pass in a list,list element as the Set element

>>> s=set ([' A ', ' B ', ' C '])

2.3 Access

Because set stores unordered collections , we cannot access them through an index.

Accessing an element in a set actually determines whether an element is in the set.

>>> ' Bart ' in S

False

2.4 Traverse

for-in Traverse

2.5 Update

because set stores a set of unordered elements that are not duplicated , the update set mainly does two things:

One is to add the new element to the set, and the other is to remove the existing element from the set.

added : S.add (object)

Delete : S.remove (object) # Note: Delete If the element does not exist, an error will be added.

Python Basics--Dict&set

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.