详解python itertools功能

作者:neweastsun 时间:2022-12-05 23:07:31 

介绍

      itertools是python内置的模块,使用简单且功能强大,这里尝试汇总整理下,并提供简单应用示例;如果还不能满足你的要求,欢迎加入补充。

      使用只需简单一句导入:import itertools

chain()

      与其名称意义一样,给它一个列表如 lists/tuples/iterables,链接在一起;返回iterables对象。


letters = ['a', 'b', 'c', 'd', 'e', 'f']
booleans = [1, 0, 1, 0, 0, 1]
  print(list(itertools.chain(letters,booleans)))
#   ['a', 'b', 'c', 'd', 'e', 'f', 1, 0, 1, 0, 0, 1]

print(tuple(itertools.chain(letters,letters[3:])))
#   ('a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f')

print(set(itertools.chain(letters,letters[3:])))
#   {'a', 'd', 'b', 'e', 'c', 'f'}

print(list(itertools.chain(letters,letters[3:])))
#   ['a', 'b', 'c', 'd', 'e', 'f', 'd', 'e', 'f']

for item in list(itertools.chain(letters,booleans)):
   print(item)

count()

  生成 * 限序列,count(start=0, step=1) ,示例从100开始,步长为2,循环10,打印对应值;必须手动break,count()会一直循环。


 i = 0
 for item in itertools.count(100,2):
   i += 1
   if i > 10 : break

print(item)

filterfalse ()
  Python filterfalse(contintion,data) 迭代过滤条件为false的数据。如果条件为空,返回data中为false的项;
booleans = [1, 0, 1, 0, 0, 1]
numbers = [23, 20, 44, 32, 7, 12]

print(list(itertools.filterfalse(None,booleans)))
#   [0, 0, 0]
print(list(itertools.filterfalse(lambda x : x < 20,numbers)))
#  [23, 20, 44, 32]

compress()

返回我们需要使用的元素,根据b集合中元素真值,返回a集中对应的元素。


print(list(itertools.compress(letters,booleans)))
# ['a', 'c', 'f']

starmap()

      针对list中的每一项,调用函数功能。starmap(func,list[]) ;


starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000

>>> from itertools import *
>>> x = starmap(max,[[5,14,5],[2,34,6],[3,5,2]])
>>> for i in x:
>>> print (i)
14
34
5
repeat()
repeat(object[, times]) 重复times次;
repeat(10, 3) --> 10 10 10
dropwhile()
dropwhile(func, seq );当函数f执行返回假时, 开始迭代序列
dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
takewhile()
takewhile(predicate, iterable);返回序列,当predicate为true是截止。
takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
islice()
islice(seq[, start], stop[, step]);返回序列seq的从start开始到stop结束的步长为step的元素的迭代器
for i in islice("abcdef", 0, 4, 2):#a, c
 print i

product()

product(iter1,iter2, ... iterN, [repeat=1]);创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔积的元组,repeat是一个关键字参数,指定重复生成序列的次数

   


# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
 # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
for i in product([1, 2, 3], [4, 5], [6, 7]):
 print i
(1, 4, 6)
(1, 4, 7)
(1, 5, 6)
(1, 5, 7)
(2, 4, 6)
(2, 4, 7)
(2, 5, 6)
(2, 5, 7)
(3, 4, 6)
(3, 4, 7)
(3, 5, 6)
(3, 5, 7)

permutations()

permutations(p[,r]);返回p中任意取r个元素做排列的元组的迭代器


for i in permutations([1, 2, 3], 3):
 print i
(1, 2, 3)
(1, 3, 2)
(2, 1, 3)
(2, 3, 1)
(3, 1, 2)
(3, 2, 1)

combinations()

combinations(iterable,r);创建一个迭代器,返回iterable中所有长度为r的子序列,返回的子序列中的项按输入iterable中的顺序排序

note:不带重复


for i in combinations([1, 2, 3], 2):
 print i
(1, 2)
(1, 3)
(2, 3)
combinations_with_replacement()

同上, 带重复 例子:


for i in combinations_with_replacement([1, 2, 3], 2):
 print i
(1, 1)
(1, 2)
(1, 3)
(2, 2)
(2, 3)
(3, 3)

应用示例

求质数序列中1,3,5,7,9,11,13,15三个数之和为35的三个数;


def get_three_data(data_list,amount):
 for data in list(itertools.combinations(data_list, 3)):
   if sum(data) == amount:
     print(data)
#(7, 13, 15)
#(9, 11, 15)

总结

以上所述是小编给大家介绍的python itertools功能,希望对大家有所帮助!

来源:https://blog.csdn.net/neweastsun/article/details/51965226

标签:python,itertools
0
投稿

猜你喜欢

  • 利用Python进行图像的加法,图像混合(附代码)

    2021-06-22 20:28:44
  • Python中切片的详细操作篇

    2021-11-29 13:33:26
  • Python 调用PIL库失败的解决方法

    2023-01-25 02:47:08
  • Python通过4种方式实现进程数据通信

    2023-11-04 15:13:48
  • python实现划词翻译

    2023-04-28 19:36:54
  • 浅析Python面向对象编程

    2023-11-23 07:52:12
  • asp如何写入超长的字符串?

    2010-06-09 18:53:00
  • asp下过滤非法的SQL字符的函数代码

    2011-03-03 11:23:00
  • pytorch中的torch.nn.Conv2d()函数图文详解

    2021-02-01 17:32:13
  • Django uwsgi Nginx 的生产环境部署详解

    2023-10-21 14:55:30
  • SQL注入宽字节注入由浅到深学习

    2023-06-14 04:16:40
  • django框架模型层功能、组成与用法分析

    2021-10-21 22:00:28
  • python基础教程之简单入门说明(变量和控制语言使用方法)

    2023-01-06 00:25:20
  • 给页面 unload 加上效果

    2009-03-12 12:32:00
  • 利用pandas将非数值数据转换成数值的方式

    2023-05-18 15:17:17
  • 再读《你是一个职业的页面重构工作者吗?》

    2009-02-11 12:22:00
  • 在Django的View中使用asyncio的方法

    2022-04-10 13:16:41
  • python3 字符串str和bytes相互转换

    2023-03-31 09:12:47
  • 探讨链接打开方式

    2009-03-16 16:55:00
  • sqlserver isnull在数据库查询中的应用

    2011-12-01 10:30:25
  • asp之家 网络编程 m.aspxhome.com