侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python标准库itertools的使用方法

Python  /  管理员 发布于 7年前   182

Python标准库itertools模块介绍

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

使用Python标准库itertools只需简单一句导入: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)14345

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 111for 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)

更多python标准库使用方法请点击下面的相关文章


  • 上一条:
    使用Python来做一个屏幕录制工具的操作代码
    下一条:
    Python实现投影法分割图像示例(二)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客