文章详情页
python - 所有可能的排列组合问题
浏览:89日期:2022-07-21 09:22:02
问题描述
暂且理解为一个字符串中字母的所有组合方法,如下,暴力而又丑陋的穷举法。。。想请教下有没有什么更好的方法,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. 求大神帮我看看是哪里写错了 感谢细心解答2. javascript - 数组原声方法中的一段代码3. html - eclipse 标签错误4. php自学从哪里开始?5. python - from ..xxxx import xxxx到底是什么意思呢?6. 网页爬虫 - python爬虫翻页问题,请问各位大神我这段代码怎样翻页,还有价格要登陆后才能看到,应该怎么解决7. 数据库 - MySQL 单表500W+数据,查询超时,如何优化呢?8. javascript - JS设置Video视频对象的currentTime时出现了问题,IE,Edge,火狐,都可以设置,反而chrom却...9. phpstady在win10上运行10. github - 利用Python 自动化部署问题
排行榜
