博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python之itertools的排列组合相关
阅读量:7088 次
发布时间:2019-06-28

本文共 1200 字,大约阅读时间需要 4 分钟。

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

import itertoolst_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---

转载地址:http://tfwql.baihongyu.com/

你可能感兴趣的文章
android progressbar 水平进度条
查看>>
解决yum升级的问题“There was a problem importing one of the Python modules”
查看>>
kettle_删除“共享输出表”引发的错误
查看>>
ds18b20再硬件设计部分的注意事项
查看>>
SpringMVC和Struts2的区别
查看>>
windows下使用tomcat部署网站
查看>>
算法的5个特点
查看>>
php 执行命令函数
查看>>
tcp三次握手 redis 连接超时
查看>>
栅格数据可视化资源
查看>>
字符串分隔
查看>>
Matlab GUI保存图片
查看>>
Android权限之sharedUserId和签名
查看>>
Edit Distance (编辑距离) .NET 实现
查看>>
GOLANG 声明
查看>>
mybatis 延迟加载
查看>>
java blob 文件上传下载
查看>>
git推送代码Gogs报401错误
查看>>
第一讲-认识mongoDB
查看>>
Codeforces Round #439 (Div. 2)
查看>>