文章详情页
python - 所有可能的排列组合问题
问题描述
暂且理解为一个字符串中字母的所有组合方法,如下,暴力而又丑陋的穷举法。。。想请教下有没有什么更好的方法,itertools中的几种方法都试过了,没有符合我想要的方法,谢谢!
base=’ATCG’list=[]for i in base: for j in base:for k in base: for m in base:for l in base: for n in base:seq=i+j+k+m+l+nlist.append(seq)print(len(set(list)))4096
问题解答
回答1:# coding: utf8from itertools import productbase = ’ATCG’result = product(base, repeat=6) # 因为内容太多, 所以返回生成器, 可以用list方法使其变成列表print(len(set(result)))# --- 结果 ----4096回答2:
import itertoolslen(list(itertools.product(base, repeat=6)))回答3:
from itertools import productprint(list(map(''.join, product('ATCG', repeat=6))))
相关文章:
1. node.js - 怎么在初始化的时候使得一个子组件不加载?2. 代码如下,java用BigDecimal四舍五入怎么还有问题呀?哪种方式是完全没有问题的?3. angular.js - 请教angularjs里html和js通信的问题4. 求解决方法:Parse error: syntax error, unexpected5. 新人做网站求帮助6. java - mybatis里的REGEXP7. Linux服务器下怎么把phpStudy卸载干净8. 请问连接文件怎么写9. 没太明天这样写的作用10. mysql - thinkphp5 在MAC电脑本地正常,部署LINUX服务器之后,模型不存在
排行榜