python - 所有可能的排列组合问题
本文介绍了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+n
list.append(seq)
print(len(set(list)))
4096
解决方案
# coding: utf8
from itertools import product
base = 'ATCG'
result = product(base, repeat=6) # 因为内容太多, 所以返回生成器, 可以用list方法使其变成列表
print(len(set(result)))
# --- 结果 ----
4096
这篇关于python - 所有可能的排列组合问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文