python中的itertools.Permutations()

itertool是python提供的一个模块,用于创建有效循环的迭代器。他提供了于迭代器一起使用的各种函数,以产生复杂的迭代器,并帮助我们轻松,高校的解决时间和内存方面的问题,itertools模块为我们提供多种方法来操控遍历的序列
itertools.permutation()函数属于组合发电机,用于简化组合结构(例如排列,组合,笛卡尔积)的递归生成器成为组合迭代器
用法:Permutation(iterator,r)
实例1:
from itertools import permutations
a =“GEFK”
# no length entered so default lenth
#taken as 4 (the length of string Gefk)
p =permutations(a)
$print the obtained permutations
for j in list §
print(j)
输出:
(‘G’, ‘e’, ‘E’, ‘K’)
(‘G’, ‘e’, ‘K’, ‘E’)
(‘G’, ‘E’, ‘e’, ‘K’)
(‘G’, ‘E’, ‘K’, ‘e’)
(‘G’, ‘K’, ‘e’, ‘E’)
(‘G’, ‘K’, ‘E’, ‘e’)
(‘e’, ‘G’, ‘E’, ‘K’)
(‘e’, ‘G’, ‘K’, ‘E’)
(‘e’, ‘E’, ‘G’, ‘K’)
(‘e’, ‘E’, ‘K’, ‘G’)
(‘e’, ‘K’, ‘G’, ‘E’)
(‘e’, ‘K’, ‘E’, ‘G’)
(‘E’, ‘G’, ‘e’, ‘K’)
(‘E’, ‘G’, ‘K’, ‘e’)
(‘E’, ‘e’, ‘G’, ‘K’)
(‘E’, ‘e’, ‘K’, ‘G’)
(‘E’, ‘K’, ‘G’, ‘e’)
(‘E’, ‘K’, ‘e’, ‘G’)
(‘K’, ‘G’, ‘e’, ‘E’)
(‘K’, ‘G’, ‘E’, ‘e’)
(‘K’, ‘e’, ‘G’, ‘E’)
(‘K’, ‘e’, ‘E’, ‘G’)
(‘K’, ‘E’, ‘G’, ‘e’)
(‘K’, ‘E’, ‘e’, ‘G’)
实例二:
from itertools import permutations
print(“All the permutations of the given list is:")
print (list(pertations([1,‘geeks’],2)))
print ()
print(“All the permutations of the given string is:”)
print( list (permutations('AB’)))
print()
print(”All the permutation of the given container is :“)
print(List(permutation(range(3),2)))
输出:
All the permutations of the given list is:
[(1, ‘geeks’), (‘geeks’, 1)]

All the permutations of the given string is:
[(‘A’, ‘B’), (‘B’, ‘A’)]

All the permutations of the given container is:
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值