參考: http://blog.csdn.net/xiaocaiju/article/details/6968123 1. chain的使用 - import itertools
- listone = ['a','b','c']
- listtwo = ['11','22','abc']
- for item in itertools.chain(listone,listtwo):
- print item
輸出:a b c 11 22 abc
2. count的使用
- i = 0
- for item in itertools.count(100):
- if i>10:
- break
- print item,
- i = i+1
-
功能:從100開始數(shù)10個(gè)數(shù),cout返回一個(gè)無界的迭代器,小菜鳥我不會(huì)控制了,就引入了一個(gè)計(jì)數(shù)I,讓它計(jì)數(shù)10次。。
輸出:100 101 102 103 104 105 106 107 108 109 110
3.cycle的使用
- import itertools
- listone = ['a','b','c']
- listtwo = ['11','22','abc']
-
- for item in itertools.cycle(listone):
- print item,
打印出a b c a b c a b c a b c a b c a b c a b c a b c a b c...
功能:從列表中取元素,到列表尾后再?gòu)念^取...
無限循環(huán),因?yàn)閏ycle生成的是一個(gè)無界的失代器
4.ifilter的使用
ifilter(fun,iterator)
返回一個(gè)可以讓fun返回True的迭代器,
- import itertools
- listone = ['a','b','c']
- listtwo = ['11','22','abc']
-
- def funLargeFive(x):
- if x > 5:
- return True
-
- for item in itertools.ifilter(funLargeFive,range(-10,10)):
- print item,
結(jié)果:6 7 8 9
5. imap的使用
imap(fun,iterator)
返回一個(gè)迭代器,對(duì)iterator中的每個(gè)項(xiàng)目調(diào)用fun
- import itertools
- listone = ['a','b','c']
- listtwo = ['11','22','abc']
- listthree = [1,2,3]
- def funAddFive(x):
- return x + 5
- for item in itertools.imap(funAddFive,listthree):
- print item,
返回:6 7 8 對(duì)listthree中的元素每個(gè)加了5后返回給迭代器
6.islice的使用
islice()(seq, [start,] stop [, step])
- import itertools
- listone = ['a','b','c']
- listtwo = ['11','22','abc']
- listthree = listone + listtwo
- for item in itertools.islice(listthree,3,5):
- print item,
功能:返回迭代器,其中的項(xiàng)目來自 將seq,從start開始,到stop結(jié)束,以step步長(zhǎng)切割后
打印出:11 22
7.izip的使用
izip(*iterator)
- import itertools
- listone = ['a','b','c']
- listtwo = ['11','22','abc']
- listthree = listone + listtwo
- for item in itertools.izip(listone,listtwo):
- print item,
結(jié)果:('a', '11') ('b', '22') ('c', 'abc')
功能:返回迭代器,項(xiàng)目是元組,元組來自*iterator的組合
8. repeate
repeate(elem [,n])
- import itertools
- listone = ['a','b','c']
- for item in itertools.repeat(listone,3):
- print item,
結(jié)果:['a', 'b', 'c'] ['a', 'b', 'c'] ['a', 'b', 'c']
9. 參考:http://blog.csdn.net/xiaocaiju/article/details/6969401 生成器表達(dá) 式是用來生成函數(shù)調(diào)用時(shí)序列參數(shù)的一種迭代器寫法
生成器對(duì)象可以遍歷或轉(zhuǎn)化為列表(或元組等數(shù)據(jù)結(jié)構(gòu)),但不能切片(slicing)。當(dāng)函數(shù)的唯一的實(shí)參是可迭代序列時(shí),便可以去掉生成器表達(dá)式兩端>的圓括號(hào),寫出更優(yōu)雅的代碼:
>>>> sum(i for i in xrange(10))
45
sum聲明:
sum( iterable[,
start])
Sums start and the items of an iterable from left to right and returns the total.
start defaults to 0. The
iterable‘s items are normally numbers, and are not allowed to
be strings. The fast, correct way to concatenate a sequence of strings
is by calling
''.join(sequence). Note that
sum(range(n),
m) is equivalent to reduce(operator.add,
range(n), m) To add floating point values with extended precision, see
math.fsum().
參數(shù)要求傳入 可迭代序列,我們傳入一個(gè)生成器對(duì)象,完美實(shí)現(xiàn)。。。
注意區(qū)分下面代碼:
上面的j為生成器類型,下面的j為list類型
- j = (i for i in range(10))
- print j,type(j)
- print '*'*70
-
- j = [i for i in range(10)]
- print j,type(j)
結(jié)果:
at 0x01CB1A30>
**********************************************************************
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|