对python中的高效迭代器函数详解


Posted in Python onOctober 18, 2018

python中内置的库中有个itertools,可以满足我们在编程中绝大多数需要迭代的场合,当然也可以自己造轮子,但是有现成的好用的轮子不妨也学习一下,看哪个用的顺手~

首先还是要先import一下:

#import itertools
from itertools import * #最好使用时用上面那个,不过下面的是为了演示比较
  常用的,所以就直接全部导入了

一.无限迭代器:

对python中的高效迭代器函数详解

由于这些都是无限迭代器,因此使用的时候都要设置终止条件,不然会一直运行下去,也就不是我们想要的结果了。

1、count()

可以设置两个参数,第一个参数为起始点,且包含在内,第二个参数为步长,如果不设置第二个参数则默认步长为1

for x in count(10,20):
 if x < 200:
 print x
def count(start=0, step=1):
 # count(10) --> 10 11 12 13 14 ...
 # count(2.5, 0.5) -> 2.5 3.0 3.5 ...
 n = start
 while True:
 yield n
 n += step

2、cycle()

可以设置一个参数,且只接受可以迭代的参数,如列表,元组,字符串。。。,该函数会对可迭代的所有元素进行循环:

for i,x in enumerate(cycle('abcd')):
 if i < 5:
 print x
def cycle(iterable):
 # cycle('ABCD') --> A B C D A B C D A B C D ...
 saved = []
 for element in iterable:
 yield element
 saved.append(element)
 while saved:
 for element in saved:
  yield element

3、repeat()

可以设置两个参数,其中第一个参数要求可迭代,第二个参数为重复次数,第二个参数如不设置则无限循环,一般来说使用时都会设置第二个参数,用来满足预期重复次数后终止:

#注意如果不设置第二个参数notebook运行可能会宕机
for x in repeat(['a','b','c'],10):
 print x

二.有限迭代器

对python中的高效迭代器函数详解

1、chain()

可以接受不定个数个可迭代参数,不要求可迭代参数类型相同,会返回一个列表,这个类似于list的extend,不过不同点是list的extend是对原变量进行改变不返回,而chain则是就地改变并返回:

list(chain(range(4),range(5)))

list(chain(range(4),'abc'))

list(chain(('a','b','c'),'nihao',['shijie','zhongguo']))
def chain(*iterables):
 # chain('ABC', 'DEF') --> A B C D E F
 for it in iterables:
 for element in it:
  yield element

2.compress()

第一个参数为可迭代类型,第二个参数为0和1的集合,两者长度可以不等,

这个暂时不知道可以用在哪里、

list(compress(['a','b','c','d','e'],[0,1,1,1,0,1]))
def compress(data, selectors):
 # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
 return (d for d, s in izip(data, selectors) if s)

3.dropwhile()

接受两个参数,第一个参数为一个判断类似于if语句的函数,丢弃满足的项,直到第一个不满足的项出现时停止丢弃,就是

#伪代码大概是这个样子的
if condition:
 drop element
 while not condition:
 stop drop
list(dropwhile(lambda x:x>5,range(10,0,-1)))
def dropwhile(predicate, iterable):
 # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
 iterable = iter(iterable)
 for x in iterable:
 if not predicate(x):
  yield x
  break
 for x in iterable:
 yield x

4.groupby

对给定可迭代集合(有重复元素)进行分组,返回的是一个元组,元组的第一个为分组的元素,第二个为分组的元素集合,还是看代码吧:

for x,y in groupby(['a','a','b','b','b','b','c','d','e','e']):
 print x
 print list(y)
 print ''

out:
 a
 ['a', 'a']

 b
 ['b', 'b', 'b', 'b']

 c
 ['c']

 d
 ['d']

 e
 ['e', 'e']
class groupby(object):
 # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
 # [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
 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) # Exit on StopIteration
  self.currkey = self.keyfunc(self.currvalue)
 self.tgtkey = self.currkey
 return (self.currkey, self._grouper(self.tgtkey))
 def _grouper(self, tgtkey):
 while self.currkey == tgtkey:
  yield self.currvalue
  self.currvalue = next(self.it) # Exit on StopIteration
  self.currkey = self.keyfunc(self.currvalue)

5.ifilter()

这个有点像是filter函数了,不过有点不同,filter返回的是一个完成后的列表,而ifilter则是一个生成器,使用的yield

#这样写只是为了更清楚看到输出,其实这么写就跟filter用法一样了,体现不到ifilter的优越之处了
list(ifilter(lambda x:x%2,range(10)))

6.ifilterfalse()

这个跟ifilter用法很像,只是两个是相反数的关系。

list(ifilterfalse(lambda x:x%2,range(10)))

7.islice()

接受三个参数,可迭代参数,起始切片点,结束切片点,最少给定两个参数,当只有两个参数为默认第二个参数为结束切片点:

In: list(islice(range(10),2,None))
Out: [2, 3, 4, 5, 6, 7, 8, 9]

In: list(islice(range(10),2))
Out: [0, 1]

8.imap()

接受参数个数跟目标函数有关:

#接受两个参数时
list(imap(abs,range(-5,5)))

#接受三个参数时
list(imap(pow,range(-5,5),range(10)))

#接受四个参数时
list(imap(lambda x,y,z:x+y+z,range(10),range(10),range(10)))

9.starmap()

这个是imap的变异,即只接受两个参数,目标函数会作用在第二个参数集合中、

in: list(starmap(pow,[(1,2),(2,3)]))
out: [1, 8]

10.tee()

接受两个参数,第一个参数为可迭代类型,第二个为int,如果第二个不指定则默认为2,即重复两次,有点像是生成器repeat的生成器类型,

这个就有意思了,是双重生成器输出:

for x in list(tee('abcde',3)):
 print list(x)

11.takewhile()

这个有点跟dropwhile()很是想象,一个是丢弃,一个是拿取:

伪代码为:

if condition:
 take this element
 while not condition:
 stop take

eg:

in: list(takewhile(lambda x:x<10,(1,9,10,11,8)))
out: [1, 9]

12.izip()

这个跟imap一样,只不过imap是针对map的生成器类型,而izip是针对zip的:

list(izip('ab','cd'))

13.izip_longest

针对izip只截取最短的,这个是截取最长的,以None来填充空位:

list(izip_longest('a','abcd'))

三、组合迭代器

对python中的高效迭代器函数详解

1.product()

这个有点像是多次使用for循环,两者可以替代。

list(product(range(10),range(10)))

#本质上是这种的生成器模式
L = []
for x in range(10):
 for y in range(10):
 L.append((x,y))

2.permutations()

接受两个参数,第二个参数不设置时输出的没看出来是什么鬼,

第二个参数用来控制生成的元组的元素个数,而输出的元组中最后一个元素是打乱次序的,暂时也不知道可以用在哪

list(permutations(range(10),2))

3.combinations()

用来排列组合,抽样不放回,第二个参数为参与排列组合的个数

list(combinations('abc',2))
def combinations(iterable, r):
 # combinations('ABCD', 2) --> AB AC AD BC BD CD
 # combinations(range(4), 3) --> 012 013 023 123
 pool = tuple(iterable)
 n = len(pool)
 if r > n:
 return
 indices = range(r)
 yield tuple(pool[i] for i in indices)
 while True:
 for i in reversed(range(r)):
  if indices[i] != i + n - r:
  break
 else:
  return
 indices[i] += 1
 for j in range(i+1, r):
  indices[j] = indices[j-1] + 1
 yield tuple(pool[i] for i in indices)
def combinations(iterable, r):
 pool = tuple(iterable)
 n = len(pool)
 for indices in permutations(range(n), r):
 if sorted(indices) == list(indices):
  yield tuple(pool[i] for i in indices)

4.combinations_with_replacement()

与上一个的用法不同的是抽样是放回的

def combinations_with_replacement(iterable, r):
 # combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC
 pool = tuple(iterable)
 n = len(pool)
 if not n and r:
 return
 indices = [0] * r
 yield tuple(pool[i] for i in indices)
 while True:
 for i in reversed(range(r)):
  if indices[i] != n - 1:
  break
 else:
  return
 indices[i:] = [indices[i] + 1] * (r - i)
 yield tuple(pool[i] for i in indices)
def combinations_with_replacement(iterable, r):
 pool = tuple(iterable)
 n = len(pool)
 for indices in product(range(n), repeat=r):
 if sorted(indices) == list(indices):
  yield tuple(pool[i] for i in indices)

以上这篇对python中的高效迭代器函数详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用gzip模块压缩文件的简单教程
Apr 08 Python
win10系统中安装scrapy-1.1
Jul 03 Python
python实现扫描ip地址的小程序
Apr 16 Python
python 列表输出重复值以及对应的角标方法
Jun 11 Python
Python hashlib加密模块常用方法解析
Dec 18 Python
TensorFlow:将ckpt文件固化成pb文件教程
Feb 11 Python
python GUI库图形界面开发之PyQt5动态加载QSS样式文件
Feb 25 Python
3种适用于Python的疯狂秘密武器及原因解析
Apr 29 Python
基于python实现模拟数据结构模型
Jun 12 Python
Python HTMLTestRunner如何下载生成报告
Sep 04 Python
Python WSGI 规范简介
Apr 11 Python
python模块与C和C++动态库相互调用实现过程示例
Nov 02 Python
对Python中内置异常层次结构详解
Oct 18 #Python
Python运维开发之psutil库的使用详解
Oct 18 #Python
python实现自动登录后台管理系统
Oct 18 #Python
python 对给定可迭代集合统计出现频率,并排序的方法
Oct 18 #Python
python实现简单登陆系统
Oct 18 #Python
Python字典中的键映射多个值的方法(列表或者集合)
Oct 17 #Python
python字典值排序并取出前n个key值的方法
Oct 17 #Python
You might like
中国第一家无线电行
2021/03/01 无线电
简单了解PHP编程中数组的指针的使用
2015/11/30 PHP
PHP实现基本留言板功能原理与步骤详解
2020/03/26 PHP
JQuery的ajax基础上的超强GridView展示
2009/09/18 Javascript
jquery mobile changepage的三种传参方法介绍
2013/09/13 Javascript
Nodejs+express+html5 实现拖拽上传
2014/08/08 NodeJs
巧用Javascript的逻辑运算符
2016/12/02 Javascript
微信小程序 支付功能实现PHP实例详解
2017/05/12 Javascript
VUE-Table上绑定Input通过render实现双向绑定数据的示例
2018/08/27 Javascript
swiper在angularjs中使用循环轮播失效的解决方法
2018/09/27 Javascript
原生JS实现动态添加新元素、删除元素方法
2019/05/05 Javascript
nuxt框架中对vuex进行模块化设置的实现方法
2019/09/06 Javascript
解决 window.onload 被覆盖的问题方法
2020/01/14 Javascript
React中Ref 的使用方法详解
2020/04/28 Javascript
解决Vue @submit 提交后不刷新页面问题
2020/07/18 Javascript
[01:59][TI9趣味视频] 全明星赛奖励
2019/08/23 DOTA
[01:03:41]完美世界DOTA2联赛PWL S3 DLG vs Phoenix 第一场 12.17
2020/12/19 DOTA
Python写的贪吃蛇游戏例子
2014/06/16 Python
使用apidocJs快速生成在线文档的实例讲解
2018/02/07 Python
解决Python requests库编码 socks5代理的问题
2018/05/07 Python
Centos7 下安装最新的python3.8
2019/10/28 Python
记一次pyinstaller打包pygame项目为exe的过程(带图片)
2020/03/02 Python
keras实现调用自己训练的模型,并去掉全连接层
2020/06/09 Python
Pyinstaller加密打包应用的示例代码
2020/06/11 Python
如何在Python3中使用telnetlib模块连接网络设备
2020/09/21 Python
Pycharm操作Git及GitHub的步骤详解
2020/10/27 Python
Python爬虫之Selenium库的使用方法
2021/01/03 Python
html5中canvas学习笔记1-画板的尺寸与实际显示尺寸
2013/01/06 HTML / CSS
HTML5+Canvas+CSS3实现齐天大圣孙悟空腾云驾雾效果
2016/04/26 HTML / CSS
GE设备配件:GE Appliance Parts(家电零件、配件和滤水器)
2018/11/28 全球购物
爱尔兰旅游网站:ebookers.ie
2020/01/24 全球购物
.net软件工程师面试题
2015/03/31 面试题
校园公益广告语
2014/03/13 职场文书
2014个人四风对照检查材料思想汇报
2014/09/18 职场文书
个人年终总结怎么写
2015/03/09 职场文书
MySQL系列之五 视图、存储函数、存储过程、触发器
2021/07/02 MySQL