打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
python中的itertools.Permutations()
userphoto

2022.11.26 新疆

关注

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)]

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
迭代器神器 Python Itertools 大揭秘:如何让你的代码瞬间提升!
python itertools功能详解
16个鲜为人知的 Python 小众技巧,让你的代码更加简洁高效
第39天: Python itertools 模块
浅析Python迭代器的高级用法
算法创作|纸牌三角形
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服