Python built-in container (2)--Dictionary, iterator, list parsing

Source: Internet
Author: User

1. Iterator iterator

iter function, constructs a collection.

The next method returns the value of the iterator in turn

In [all]: Li

OUT[42]: [1, 2, 3, 4, 5]

In [P]: it = iter (LI)

In []: It.next ()

OUT[44]: 1

In [the]: It.next ()

OUT[45]: 2

When all the elements are returned, a Stopiterator exception is thrown.

List,tuple,set all belong to the collection.

For statement, when a list is passed to a for statement, the for statement actually converts the list to an iterator and executes the iterator implicitly.


2. Dictionary Dict

2.1 Definition and initialization in [the]: D = {}

in [+]: type (d)

OUT[47]: Dict

In []: D = {' A ': 1, ' V ': ' K '}

in [+]: D

OUT[49]: {' A ': 1, ' V ': ' K '}

In the dictionary, key is not allowed to be duplicated, so the key in the dictionary must be a hash object.

2.2 Operation of the dictionary

2.2.1 Keys Returns all keys of the dictionary as a list in []: D

OUT[50]: {' A ': 1, ' V ': ' K '}

In [Wuyi]: D.keys ()

OUT[51]: [' a ', ' V ']

2.2.2 Iterkeys Returns all keys of the dictionary as an iterator

In []: D.iterkeys ()

OUT[52]: <dictionary-keyiterator at 0x12ff7e0>

in [+]: it = D.iterkeys ()

In [si]: It.next ()

OUT[54]: ' A '

2.2.3 Values Returns a list that is the value of all elements

In [C]: D

OUT[55]: {' A ': 1, ' V ': ' K '}

in [+]: d.values ()

OUT[56]: [1, ' K ']

2.2.4 Items , returns a list in which each element is a tuple, and two values in the tuple are key and value, respectively

In [the]: D

OUT[57]: {' A ': 1, ' V ': ' K '}

in [+]: D.items ()

OUT[58]: [(' A ', 1), (' V ', ' K ')]


Traverse a dictionary:

in [+]: for V in D.values ():

....:

Print V

....:

1

K

In [MAX]: for k,v in D.items ():

....:

Print "%s =%s"% (K,V)

....:

A = 1

v = k


2.3.5 Get , value by key, returns if present, otherwise none

In [a]: D.get (' a ')

OUT[61]: 1

You can also pass a second argument to get, which means that if the key does not exist, a value is returned.

In [x]: D.get (' xx ', 100)

OUT[65]: 100


2.2.6 Has_key to determine if a key exists

in [+]: D.has_key (' a ')

OUT[63]: True

in [+]: D.has_key (' AA ')

OUT[64]: False


2.3 Add a key-value pair to the dictionary:

In [all]: D = {}

In []: d[' c '] = 1

In []: D

OUT[68]: {' c ': 1}

In []: d[' xx '] = 100

In []: dout[70]: {' C ': 1, ' xx ': 100}

In []: d[' xx '] = 200

In []: D

OUT[72]: {' C ': 1, ' xx ': 200}


2.4update operation

In []: D

OUT[73]: {' C ': 1, ' xx ': 200}

In [D.update]: ({' A ': 1, ' B ': 2})

In []: D

OUT[75]: {' A ': 1, ' B ': 2, ' C ': 1, ' xx ': 200}


The 2.5 python dictionary is a reference value , such as:

In []: D

OUT[76]: {' A ': 1, ' B ': 2, ' C ': 1, ' xx ': 200}

In [all]: D1 = d

in [+]: d1[' a '] = 200

in [+]: D1

OUT[79]: {' A ': $, ' B ': 2, ' C ': 1, ' xx ': 200}

in [+]: D

OUT[80]: {' A ': $, ' B ': 2, ' C ': 1, ' xx ': 200}


2.6 Copy operation: re-copy a dictionary

In [Bayi]: D

OUT[81]: {' A ': $, ' B ': 2, ' C ': 1, ' xx ': 200}

In [D2]: = D.copy ()

In [D2]:

OUT[83]: {' A ': $, ' B ': 2, ' C ': 1, ' xx ': 200}

in [+]: d2[' a '] = 300

In [D2]:

OUT[85]: {' A ': +, ' B ': 2, ' C ': 1, ' xx ': 200}

in [+]: D

OUT[86]: {' A ': $, ' B ': 2, ' C ': 1, ' xx ': 200}

In [ALL]: ID (d)

OUT[87]: 19662176

in [+]: ID (D2)

OUT[88]: 19501072

In []: ID (d1)

OUT[89]: 19662176


3. List resolution

[Expression for item ' in iterator] one of the most basic list parsing, returns a list

You can use the item variable in expression


Returns an iterator

(Expression for item in Terator)

In [all]: Li = [+]

In [P]: l = (x+1 for x in Li)

In [the]: l

OUT[92]: <generator object <genexpr> at 0x12fa730>

In [the]: L.next ()

OUT[93]: 2In [94]: L.next ()

OUT[94]: 3


An iterator is a lazy evaluation that is only used to calculate the value, otherwise it will not. The list is the first to find out all the values. So when the data is large, iterators have better performance.

List parsing with conditions

[Expression for item ' in iterator if condition] is append to the list and returned when the condition is met. When it becomes a parenthesis, an iterator is returned.

In [P]: Li

OUT[95]: [1, 2, 3]

In [P]: [x for X in Li if x% 2 ==0]

OUT[96]: [2]


with multiple conditions [expression for item in iterator if Conditionx if Conditiony]

With multiple iterators (Cartesian product and list parsing): [Expr for x in Iterx for y in Itery], similar to two nested for loop operations

In [£ º]: [(x, Y) for x in [All-in-A-z] for y in [1,3]]

OUT[99]: [(1, 1), (1, 3), (2, 1), (2, 3), (3, 1), (3, 3)]

The performance of list parsing is much higher than the loop statement.


This article from the "Technology life, Simple not simple" blog, please be sure to keep this source http://willis.blog.51cto.com/11907152/1855129

Python built-in container (2)--Dictionary, iterator, list parsing

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.