您的位置:首页 > 编程语言 > Python开发

python中itertools模块介绍---02

2015-03-29 21:15 561 查看
chain(*iterables):

源代码:

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

chain函数接收多个参数(iterables),并且对iterables进行遍历,返回每个iterable中的元素。最终结果就像返回的所有元素均来自同一个单一的序列,例如:

>>>a=chain('ab','cd')
>>>a.next()
a
>>>a.next()
b
>>>a.next()
c
>>>a.next()
d

izip(*iterables):

源代码:

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

首先调用map函数,将iterables变为迭代器列表。当列表不为空时,再次调用map函数,对iterators中的每一个迭代器进行next,形成列表,然后变为元组返回。例如:

>>>a=izip("abc","cd")
>>>a.next()
('a','c')
>>>a.next()
(b,d)

compress(data,selectors):

源代码:

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

compress函数的实现借助了izip函数。采用生成器表达式,当izip返回的元组的第1个(从0开始)元素为真时,取该元组的第0个元素。例如:

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

dropwhile(predicate,iterable):

源代码:

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

对iterable中的每一个元素都调用predicate函数,直到predicate(x)函数返回False时,才会将该项及后续项返回。例如:

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

groupby(iterable[,key]):

源代码:

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)

groupby函数的作用是依据key对iterable进行分组。当没有指定key时,key赋值为一个匿名函数,该匿名函数返回对象本身。此时,将相邻的、且相同的元素分为一组。如"aabbcdb"将分为"aaa","bb","c","d","b"这几组。当指定key时,则按key的规则进行分组。例如key=len,那么将长度相同的元素分为一组。如a=["12","23","123","1234"]分为["12","23"],["123"],["1234"]这几组。最后返回的是一个元组(self.currkey,self._grouper(self.tgtkey))。元组的第0个元素是分组的键值,第1个元素是一个迭代器(该组中具体包含的内容)。例如:

>>>a=groupby("aaabbcde")
>>>a.next()
('a',迭代器)
>>>a.next()
('b',迭代器)
>>>a=groupby(["abc","bcd","ab","bc"],len)
>>>a.next()
(3,迭代器)
>>>a.next()
(2,迭代器)

ifilter(predicate,iterable):

源代码:

def ifilter(predicate,iterable):
if predicate is None:
predicate=bool
for x in iterable:
if predicate(x):
yield x

当predicate(x)为真时,返回x。例如:

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

iflterfalse(predicate,iterable):

与iflter函数相反,当predicate(x)为假时,x被返回。源代码和具体的实例略。

islice(iterable,stop):

islice(iterable,start,stop[,step])

源代码:

def islice(iterbale,*args):
s=slice(*args)
it=iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))
nexti=next(it)
for i,element in enumerate(iterable):
if i==nexti:
yield element
nexti=next(it)

首先创建一个slice对象,slice(start,stop[,step])。xrange(start,stop[,step]),创建了一个一维矩阵,并转换为迭代器。当iterable的下标与nexti相等时,返回element。因此,islice是根据索引来返回值例如:

>>>a=islice("abcdef",1,5,2)
>>>a.next()
b
>>>a.next()
d

>>>def myfunc():
value=raw_input().strip()
while value !='':
for el in islice(value.split(),2,None):
yield el
value=raw_input().strip()
>>>a=myfunc()
>>>a.next()
a b c d
'c'
>>>a.next()
'd'

imap(function,*iterables):

源代码:

def imap(function,*iterables):
iterables=map(iter,iterables)
while True:
args=[next(it) for it in iterables]
if function is None:
yield tuple(args)
else:
yield function(*args)

类似于map函数,只不过返回的是一个迭代器。并且,当iterables中的任意一个结束时,返回就终止,而不是补None。例如:

>>>a=imap(lambda x,y:x*y,[1,2,3],[3,2,1,0])
>>>a.next()
3
>>>a.next()
4
>>>a.next()
3

starmap(function,iterable):

源代码:

def starmap(function,iterable):
for args in iterable:
yield function(*args)

iterable中的每一个元素都调用function(*args)函数,并将结果返回。只有当iterable生成的项适合于这种函数是,此函数才有效,否则产生异常。例如:

>>>a=starmap(lambda x,y:x+y,[(1,2),(2,3)])
>>>a.next()
3
>>>a.next()
5

tee(iterable[,n=2]):

源代码:

def tee(iterable,n=2):
it=iter(iterable)
deques=[collection.deque() for i in range(n)]
def gen(mydeque):
while True:
if not mydeque:
newval=next(it)
for d in deques:
d.append(newval)
yield mydeque.popleft()
return tuple(gen(d) for d in deques)

从iterable创建n个独立的迭代器,创建的迭代器以n元组的形式返回,n的默认值为2,此函数适用于任何可迭代的对象。但是,为了克隆原始迭代器,生成的项会被缓存,并在所有创建的迭代器中使用。一定要注意,不要在调用tee()之后使用原始迭代器iterable,否则缓存机制可能无法正确工作。例如:

>>>a=iter([1,2])
>>>b,c=tee(a)
>>>b.next()
1
>>>c.next()
1
>>>b.next()
2
>>>c.next()
2

takewhile(predicate,iterable):

源代码:

def takewhile(predicate,iterable):
for x in iterable:
if predicate(x):
yield x
else:
break

和dropwhile相反,当predicate(x)为真时返回,为假则停止。不再举例。

izip_longest(*iterables[,fillvalue]):

与izip()相同,但是迭代过程会持续到所有输入迭代变量iter1,iter2等都耗尽为止,如果没有使用fillvalue关键字参数指定不同的值,则使用None来填充已经使用的迭代变量的值。如:

>>>a=izip_longest("ab","def",fillvalue="ok")
>>>a.next()
('a','d')
>>>a.next()
('b','e')
>>>a.next()
('ok','f')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: