相见恨晚的 Python 内置库:itertools

开发 后端
很多人都致力于把Python代码写得更Pythonic,一来更符合规范且容易阅读,二来一般Pythonic的代码在执行上也更有效率。今天就先给大家介绍一下Python的系统库itertools。

[[426404]]

 前言

最近事情不是很多,想写一些技术文章分享给大家,同时也对自己一段时间来碎片化接受的知识进行一下梳理,所谓写清楚才能说清楚,说清楚才能想清楚,就是这个道理了。

很多人都致力于把Python代码写得更Pythonic,一来更符合规范且容易阅读,二来一般Pythonic的代码在执行上也更有效率。今天就先给大家介绍一下Python的系统库itertools。

itertools库

迭代器(生成器)在Python中是一种很常用也很好用的数据结构,比起列表(list)来说,迭代器最大的优势就是延迟计算,按需使用,从而提高开发体验和运行效率,以至于在Python 3中map,filter等操作返回的不再是列表而是迭代器。

话虽这么说但大家平时用到的迭代器大概只有range了,而通过iter函数把列表对象转化为迭代器对象又有点多此一举,这时候我们今天的主角itertools就该上场了。

使用itertools

itertools中的函数大多是返回各种迭代器对象,其中很多函数的作用我们平时要写很多代码才能达到,而在运行效率上反而更低,毕竟人家是系统库。

itertools.accumulate

简单来说就是累加。 

  1. >>> import itertools  
  2. >>> x = itertools.accumulate(range(10))  
  3. >>> print(list(x))  
  4. [0, 1, 3, 6, 10, 15, 21, 28, 36, 45] 

itertools.chain

连接多个列表或者迭代器。 

  1. >>> x = itertools.chain(range(3), range(4), [3,2,1])  
  2. >>> print(list(x))  
  3. [0, 1, 2, 0, 1, 2, 3, 3, 2, 1] 

itertools.combinations

求列表或生成器中指定数目的元素不重复的所有组合 

  1. >>> x = itertools.combinations(range(4), 3)  
  2. >>> print(list(x))  
  3. [(0, 1, 2), (0, 1, 3), (0, 2, 3), (1, 2, 3)] 

itertools.combinations_with_replacement

允许重复元素的组合 

  1. >>> x = itertools.combinations_with_replacement('ABC', 2)  
  2. >>> print(list(x))  
  3. [('A', 'A'), ('A', 'B'), ('A', 'C'), ('B', 'B'), ('B', 'C'), ('C', 'C')]  

itertools.compress

按照真值表筛选元素 

  1. >>> x = itertools.compress(range(5), (True, False, True, True, False))  
  2. >>> print(list(x))  
  3. [0, 2, 3] 

itertools.count

就是一个计数器,可以指定起始位置和步长 

  1. >>> x = itertools.count(start=20step=-1)  
  2. >>> print(list(itertools.islice(x, 0, 10, 1)))  
  3. [20, 19, 18, 17, 16, 15, 14, 13, 12, 11] 

itertools.cycle

循环指定的列表和迭代器 

  1. >>> x = itertools.cycle('ABC')  
  2. >>> print(list(itertools.islice(x, 0, 10, 1)))  
  3. ['A', 'B', 'C', 'A', 'B', 'C', 'A', 'B', 'C', 'A'] 

itertools.dropwhile

按照真值函数丢弃掉列表和迭代器前面的元素 

  1. >>> x = itertools.dropwhile(lambda e: e < 5, range(10))  
  2. >>> print(list(x))  
  3. [5, 6, 7, 8, 9] 

itertools.filterfalse

保留对应真值为False的元素 

  1. >>> x = itertools.filterfalse(lambda e: e < 5, (1, 5, 3, 6, 9, 4))  
  2. >>> print(list(x))  
  3. [5, 6, 9] 

itertools.groupby

按照分组函数的值对元素进行分组 

  1. >>> x = itertools.groupby(range(10), lambda x: x < 5 or x > 8)                                                                                               
  2. >>> for condition, numbers in x:                                                 
  3. ...     print(condition, list(numbers))                                                                                                         
  4. True [0, 1, 2, 3, 4]                                                               
  5. False [5, 6, 7, 8]                                                                
  6. True [9] 

itertools.islice

上文使用过的函数,对迭代器进行切片 

  1. >>> x = itertools.islice(range(10), 0, 9, 2)  
  2. >>> print(list(x))  
  3. [0, 2, 4, 6, 8] 

itertools.permutations

产生指定数目的元素的所有排列(顺序有关) 

  1. >>> x = itertools.permutations(range(4), 3)  
  2. >>> print(list(x))  
  3. [(0, 1, 2), (0, 1, 3), (0, 2, 1), (0, 2, 3), (0, 3, 1), (0, 3, 2), (1, 0, 2), (1, 0, 3), (1, 2, 0), (1, 2, 3), (1, 3, 0), (1, 3, 2), (2, 0, 1), (2, 0,3), (2, 1, 0), (2, 1, 3), (2, 3, 0), (2, 3, 1), (3, 0, 1), (3, 0, 2), (3, 1, 0), (3, 1, 2), (3, 2, 0), (3, 2, 1)] 

 itertools.product

产生多个列表和迭代器的(积) 

  1. >>> x = itertools.product('ABC', range(3))  
  2. >>>  
  3. >>> print(list(x))  
  4. [('A', 0), ('A', 1), ('A', 2), ('B', 0), ('B', 1), ('B', 2), ('C', 0), ('C', 1), ('C', 2)] 

itertools.repeat

简单的生成一个拥有指定数目元素的迭代器 

  1. >>> x = itertools.repeat(0, 5)  
  2. >>> print(list(x))  
  3. [0, 0, 0, 0, 0] 

itertools.starmap

类似map 

  1. >>> x = itertools.starmap(str.islower, 'aBCDefGhI')  
  2. >>> print(list(x))  
  3. [True, False, False, False, True, True, False, True, False]  

itertools.takewhile

与dropwhile相反,保留元素直至真值函数值为假。 

  1. >>> x = itertools.takewhile(lambda e: e < 5, range(10))  
  2. >>> print(list(x))  
  3. [0, 1, 2, 3, 4] 

itertools.tee

这个函数我也不是很懂,似乎是生成指定数目的迭代器 

  1. >>> x = itertools.tee(range(10), 2)  
  2. >>> for letters in x:  
  3. ...     print(list(letters))  
  4. ...  
  5. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]  
  6. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

itertools.zip_longest

类似于zip,不过已较长的列表和迭代器的长度为准 

  1. >>> x = itertools.zip_longest(range(3), range(5))  
  2. >>> y = zip(range(3), range(5))  
  3. >>> print(list(x))  
  4. [(0, 0), (1, 1), (2, 2), (None, 3), (None, 4)]  
  5. >>> print(list(y))  
  6. [(0, 0), (1, 1), (2, 2)] 

结语

大概就总结到这里,不过老实说Python的各种语言特性和库还是要多用才能熟练,最终达到随手拈来的程度,装逼的说就是由术入道。 

 

责任编辑:庞桂玉 来源: Linux公社
相关推荐

2021-04-14 09:22:18

Python技巧交换变量值

2024-01-29 00:51:39

前端开发利器

2020-05-06 09:18:56

Pandas函数大数据技术

2017-06-01 09:34:55

R语言包sqldf

2019-08-09 14:13:22

Python知乎程序员

2021-08-19 15:02:32

科技软件电脑

2020-04-01 14:16:05

Pandastips数据分析

2015-10-27 10:12:21

r语言命令相见恨晚

2018-01-25 14:53:20

iPhone技巧删除照片

2015-07-14 14:09:56

2009-02-20 13:12:23

unixSolaris初学者

2020-05-07 09:02:55

代码Python开发

2016-12-09 12:50:36

Android

2019-02-12 11:22:29

Linux 系统 命令

2023-08-02 20:04:47

2018-02-07 14:54:17

Riverbed应用性能管理数字体验

2021-08-20 15:43:54

iPhone手机iOS

2021-04-20 10:47:59

手机APP科技

2019-08-29 09:10:11

Linux命令编程语言

2020-11-23 21:23:34

办公
点赞
收藏

51CTO技术栈公众号