python关于itertools的学习

import itertools
#from itertools import map
from operator import itemgetter
#建立无限迭代
nums = itertools.count(1)
#建立无限循环
my_str =itertools.cycle(["my_cycle:你瞅啥!"])
#建立无限重复
my_rep = itertools.repeat("my_repeat:瞅你咋地!")
#建立有限拼接
my_chain = itertools.chain(["加油!"],["干起来!"])
#建立无限拼接
my_chain1 = itertools.chain.from_iterable("干"*10)
#建立ok items筛选
my_compress = itertools.compress("GIOOUYDJOB",[1,0,1,1,0,0,1,1,1,1])
#寻找序列截断点并返回截断
my_dropwhile = itertools.dropwhile(lambda x: len(x)<6,['good','job','backfalseitem and subsequent items',134])
data = ['1','itertoools','2','is','3','cool!']
my_groupby = itertools.groupby(data,lambda x:x.isdigit())
my_groupby1 = []
for i,j in my_groupby:
    if i==False:
        my_groupby1.append(list(j))
print("my_groupby is:{}".format(my_groupby1))

#my_ifilter = itertools.ifilter(lambda x:x.isalpha(),data)
#此句已经被filter替代
#print("my_ifilter is:".format(list(my_ifilter)))

#imap功能于map相同,islice靠开始结束和步长切片
my_islice = list(map(str,list(itertools.islice(itertools.count(1),3,10,2))))
#my_tee复制迭代器
my_tee = itertools.tee(my_islice,2)
print("my_tee is:{}and{}".format(list(my_tee[0]),list(my_tee[1])))
#组合生成器
my_product = itertools.product("ABC",[1,2])
print("my_product is:{}".format(list(my_product)))
#生成一个顺序排列,两两组合
my_permutations = itertools.permutations("abc".upper(),2)
print("my_permutations is:{}".format(list(my_permutations)),end="\n")
#生成一个无顺序排列
my_combinations = itertools.combinations("ABC",2)
print("my_combinations is:{}".format(list(my_combinations)))
#生成一个包含自身的无顺序排列
my_combinations_with_replacement = itertools.combinations_with_replacement("abc",2)
print("my_combinations_with_replacement is;{}".format(list(my_combinations_with_replacement)))
#打印
print("my_chain is:%s,%s"%(next(my_chain),next(my_chain)))
print("my_compress is:{}".format(''.join(list(itertools.chain(list(my_compress))))))
print('my_dropwhile is:{}'.format(list(my_dropwhile)))
for i in nums:
    if i>10:
        break
    print("第%d回合"%i,end="*******")
    print(next(my_chain1))


    print(next(my_str))
    print(next(my_rep))
    ********************************************分割线************************************************************

输出:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值