Python advanced Programming-part2 efficient iterator module Itertools

Source: Internet
Author: User
Tags iterable

This article will give a brief introduction to Python's built-in iterator module.

We have learned that the relationship between an object and an iterator can be iterated. Here it may be summed up once, in anticipation of hearing a different sound,

Purely discussion. An iterative object is a type that has an iterative trait, and an iteration is a function provided by an iterator, that is, the ability to return an element at a time and return the element in turn. For example, a list or tuple is an object that can be iterated. An iterative object is then processed by the built-in ITER method to get an iterator object:

In [8]: IList = iter ([+ +]) in [9]: Type (iList) out[9]: Listiteratorin [ten]: Ilist.next () out[10]: 1In [all]: ilist.__iter__ () out[11]: <listiterator at 0x3753550>

The above content helps to understand the topic of this article--python built-in efficient iterator module itertools

islice: Window iterator

Itertools.islice (iterable, start, stop, step) returns an iterator built on a sub (iterative) sequence.

In [1]: l = [1,2,3,4,5]in [2]: import itertoolsin [3]: iter1  = itertools.islice (l, 1, 5, 2)    #在列表L的位置1 (with position 1) to position 5 (excluding position 5)                                                 #区间中以步进2选取元素构成可迭代对象 (even if only                                                    #一个元素) to generate the corresponding iterator in [4]: iter1out[4]: <itertools.islice at  0x36967c8&gt, .... In [6]: for x in iter1:   ...:     print  x   ...: 24 

Tee: Round-trip iterator

itertools.tee (iterable) returns two independent iterators that are built on top of the same iterative object.

In [1]: L = [1,2,3,4,5]in [2]: from Itertools import Teein [3]: A, B = tee (L) in [4]: A,bout[4]: (<itertools.tee at 0x36a Fd88>, <itertools.tee at 0x36afdc8>) in [5]: A.next () out[5]: 1In [6]: A.next () out[6]: 2In [7]: B.next () out[7]: 1

Examples are all about the function of the module, do not show the purpose, but also please if you use this module, do not hesitate to leave your requirements description.

Do not repeat the other, Python documents have introduced examples, this topic a little moisture.

Recommended reference:

Https://docs.python.org/2.7/library/itertools.html

Just found out that Python 2.7.9 was released. 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0002.gif "alt=" J_0002.gif "/>


This article is from the "Nameless" blog, please be sure to keep this source http://xdzw608.blog.51cto.com/4812210/1602488

Python advanced Programming-part2 efficient iterator module Itertools

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.