python itertools的使用

本文详细介绍了Python中的迭代器函数如chain、count、cycle、ifilter、imap、islice、izip和repeat的使用方法及应用场景,通过实例展示了如何在编程中灵活运用这些工具提高效率。
1. chain的使用
  1. import itertools  
  2. listone = ['a','b','c']  
  3. listtwo = ['11','22','abc']  
  4. for item in  itertools.chain(listone,listtwo):  
  5.     print item  
输出:a b c 11 22 abc

2. count的使用

    
  1. i = 0  
  2. for item in itertools.count(100):  
  3.     if i>10:  
  4.         break  
  5.     print item,  
  6.     i = i+1  
  7.       
功能:从100开始数10个数,cout返回一个无界的迭代器,小菜鸟我不会控制了,就引入了一个计数I,让它计数10次。。
输出:100 101 102 103 104 105 106 107 108 109 110

3.cycle的使用
  1. import itertools  
  2. listone = ['a','b','c']  
  3. listtwo = ['11','22','abc']  
  4.   
  5. for item in itertools.cycle(listone):  
  6.     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...
功能:从列表中取元素,到列表尾后再从头取...
无限循环,因为cycle生成的是一个无界的失代器

4.ifilter的使用
ifilter(fun,iterator)
返回一个可以让fun返回True的迭代器,
  1. import itertools  
  2. listone = ['a','b','c']  
  3. listtwo = ['11','22','abc']  
  4.   
  5. def funLargeFive(x):  
  6.     if x > 5:  
  7.         return True  
  8.       
  9. for item in itertools.ifilter(funLargeFive,range(-10,10)):  
  10.     print item,  

结果:6 7 8 9

5. imap的使用
imap(fun,iterator)
返回一个迭代器,对iterator中的每个项目调用fun
  1. import itertools  
  2. listone = ['a','b','c']  
  3. listtwo = ['11','22','abc']  
  4. listthree = [1,2,3]  
  5. def funAddFive(x):  
  6.     return x + 5  
  7. for item in itertools.imap(funAddFive,listthree):  
  8.     print item,  
返回:6 7 8  对listthree中的元素每个加了5后返回给迭代器

6.islice的使用
islice()(seq, [start,] stop [, step])
  1. import itertools  
  2. listone = ['a','b','c']  
  3. listtwo = ['11','22','abc']  
  4. listthree = listone + listtwo  
  5. for item in itertools.islice(listthree,3,5):  
  6.     print item,  
功能:返回迭代器,其中的项目来自 将seq,从start开始,到stop结束,以step步长切割后
打印出:11 22

7.izip的使用
izip(*iterator)
  1. import itertools  
  2. listone = ['a','b','c']  
  3. listtwo = ['11','22','abc']  
  4. listthree = listone + listtwo  
  5. for item in itertools.izip(listone,listtwo):  
  6.     print item,  
结果:('a', '11') ('b', '22') ('c', 'abc')
功能:返回迭代器,项目是元组,元组来自*iterator的组合


8. repeate
repeate(elem [,n])
  1. import itertools  
  2. listone = ['a','b','c']  
  3. for item in itertools.repeat(listone,3):  
  4.     print item,  

结果:['a', 'b', 'c'] ['a', 'b', 'c'] ['a', 'b', 'c']

### Python `itertools` 模块简介 Python 的 `itertools` 是一个用于操作迭代对象的工具模块,提供了许多高效循环器函数来处理无限以及有限序列。这些功能不仅提高了编程效率还优化了内存使用。 #### 基础概念 - **迭代器协议**:任何实现了 `__iter__()` 方法的对象都是可迭代对象;而实现了 `next()` 或者 `__next__()` 方法的就是迭代器。 - **惰性求值**:只有当元素被请求时才会生成下一个数据项,这使得即使对于非常大的甚至无限的数据集也能有效工作[^1]。 #### 主要组件及其用途 ##### 产品组合 (`product`) 此函数用来计算输入参数之间的笛卡尔积。可以通过设置关键字参数 `repeat` 来重复同一个集合中的成员形成多维乘积。 ```python from itertools import product char = ['a', 'b'] for each in product(char, repeat=2): print(each) # 输出: # ('a', 'a') # ('a', 'b') # ('b', 'a') # ('b', 'b') ``` 注意上述代码的实际输出应为四个二元组而不是题目描述中的三元组形式[^2]。 ##### 更多功能展示 除了 `product` 外,`itertools` 还包含了其他多个实用的功能: - **排列组合** - 排列(`permutations`):返回给定长度的所有可能顺序安排 ```python from itertools import permutations items = [1, 2, 3] for p in permutations(items): print(p) # 输出所有三个数的不同排序方式 ``` - **链式连接** 将多个可迭代对象串联成一个新的连续体。 ```python from itertools import chain chained = list(chain('ABC', 'DEF')) print(chained) # 结果: ['A', 'B', 'C', 'D', 'E', 'F'] ``` - **压缩过滤** 只保留那些满足条件的项目。 ```python from itertools import compress data = range(10) selectors = [True if i % 2 == 0 else False for i in data] filtered_data = list(compress(data, selectors)) print(filtered_data) # 结果: [0, 2, 4, 6, 8] ``` 通过以上例子可以看出,`itertools` 提供了一系列强大的工具帮助开发者更简洁优雅地编写代码,在面对复杂逻辑场景下尤其有用处。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值