python之itertools的排列组合相关

发布时间:2019-06-21 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了python之itertools的排列组合相关脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

最近由于需要做一些排列组合的需要,本来没想到python自带库中会有这功能,还花了点时间写了下,后来翻看python标准库的时候,发现,这货居然直接提供了,而且还提供了几种形式,之间上代码:

@H_777_11@import ITertools t_list = ["a","b","c","d"] PRint("product") for i in itertools.product(t_list,rePEat=2): print(i) print("permutations") for i in itertools.permutations(t_list, 2): print(i) print("combinations") for x in xrange(len(t_list)): for i in itertools.COMbinations(t_list,x+1): print(i) print("combinations_with_replacement") for i in itertools.combinations_with_replacement(t_list,2): print(i)

输入结果

product
('a', 'a')
('a', 'b')
('a', 'c')
('a', 'd')
('b', 'a')
('b', 'b')
('b', 'c')
('b', 'd')
('c', 'a')
('c', 'b')
('c', 'c')
('c', 'd')
('d', 'a')
('d', 'b')
('d', 'c')
('d', 'd')
permutations
('a', 'b')
('a', 'c')
('a', 'd')
('b', 'a')
('b', 'c')
('b', 'd')
('c', 'a')
('c', 'b')
('c', 'd')
('d', 'a')
('d', 'b')
('d', 'c')
combinations
('a',)
('b',)
('c',)
('d',)
('a', 'b')
('a', 'c')
('a', 'd')
('b', 'c')
('b', 'd')
('c', 'd')
('a', 'b', 'c')
('a', 'b', 'd')
('a', 'c', 'd')
('b', 'c', 'd')
('a', 'b', 'c', 'd')
combinations_with_replacement
('a', 'a')
('a', 'b')
('a', 'c')
('a', 'd')
('b', 'b')
('b', 'c')
('b', 'd')
('c', 'c')
('c', 'd')
('d', 'd')

很漂亮。看来还是之前某位朋友说得对,python标准库,至少得过一遍,最好能有三遍并有对应的练习,这样玩,会玩的更嗨皮~

---EOF---

脚本宝典总结

以上是脚本宝典为你收集整理的python之itertools的排列组合相关全部内容,希望文章能够帮你解决python之itertools的排列组合相关所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。