Description of the Itertools module in python---02

Source: Internet
Author: User

Chain (*iterables):

Source:

def chain (*iterables): for it in iterables:for element in It:yield element

The chain function receives multiple parameters (Iterables) and iterates over the iterables, returning the elements in each iterable. The end result is like all elements returned from the same single sequence, for example:

>>>a=chain (' AB ', ' CD ') >>>a.next () A>>>a.next () B>>>a.next () c>>> A.next () d

Izip (*iterables):

Source:

def izip (*iterables): Iterators=map (iter,iterables) while Iterators:yield tuple (map (next,iterators))

First call the map function and change the iterables to the iterator list. When the list is not empty, call the map function again, next to each iterator in iterators, form the list, and then return to the tuple. For example:

>>>a=izip ("abc", "CD") >>>a.next () (' A ', ' C ') >>>a.next () (b,d)

Compress (data,selectors):

Source:

def compress (data,selectors): return (d for D,s in Izip (data,selector) if s)

The implementation of the Compress function is aided by the Izip function. With a generator expression, the No. 0 element of the tuple is taken when the 1th (starting from 0) element of the Izip returned is true. For example:

>>>a=compress (' abc ', [0,1,2]) >>>a.next () B>>>a.next () c

Dropwhile (predicate,iterable):

Source:

def dropwhile (predicate,iterable): Iterable=iter (iterable) for x in iterable:if not predicate (x): Yield x break for x in Iterable:yield x

The predicate function is called for each element in the iterable, and the item and subsequent items are returned until the predicate (x) function returns FALSE. For example:

>>>a=dropwhile (Lambda x:x>3,[5,7,6,1,1,4]) >>>a.next () 1>>>a.next () 1>>> A.next (4)

GroupBy (Iterable[,key]):

Source:

Class groupby (object):     def __init__ (Self,iterable,key=none):         if key is None:             key=lambda x:x        self.keyfunc= Key        self.it=iter (iterable)          self.tgtkey=self.currkey=self.currvalue=object ()     def __iter__ (self):         return self    def next (self):         while self.currkey==self.tgtkey:             self.currvalue=next (self.it)              self.currkey=self.keyfunc (Self.currvalue)          self.tgtkey=self.currkey        return  (Self.currkey,self._grouper ( Self.tgtkey))     def _grouper (Self,tgtkey):         while self.currvalue==tgtkey:             yield self.currvalue             Self.currvalue=next (self.it)              Self.currkey=self.keyfunc (Self.currvalue)

The role of the GroupBy function is to group the iterable according to key. When key is not specified, key is assigned an anonymous function that returns the object itself. At this point, the adjacent, and identical, elements are grouped into one group. such as "aabbcdb" will be divided into "AAA", "BB", "C", "D", "B" these groups. When you specify key, the rules are grouped by key. For example Key=len, the elements of the same length are grouped into groups. such as a=["12", "23", "123", "1234"] are divided into ["12", "23"],["123"],["1234"] these groups. The last returned is a tuple (Self.currkey,self._grouper (Self.tgtkey)). The No. 0 element of a tuple is the key value of the grouping, and the 1th element is an iterator (the content that is contained in the group). For example:

>>>a=groupby ("Aaabbcde") >>>a.next (' A ', iterator) >>>a.next () (' B ', iterator) >>>a= GroupBy (["ABC", "BCD", "AB", "BC"],len) >>>a.next () (3, iterator) >>>a.next () (2, iterator)

IFilter (predicate,iterable):

Source:

def IFilter (predicate,iterable): If predicate is none:predicate=bool for x in ITERABLE:IF predicate ( x): Yield x

Returns x when predicate (x) is true. For example:

>>>a=ifilter (Lambda x:x>2,[1,2,3,4]) >>>a.next () 3>>>a.next () 4

Iflterfalse (predicate,iterable):

In contrast to the Iflter function, when predicate (x) is False, X is returned. The source code and the specific instance are slightly.


Description of the Itertools module in python---02

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.