您的位置:首頁技術文章
文章詳情頁

python - 所有可能的排列組合問題

瀏覽:81日期: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))))

標簽: Python 編程
相關文章:
国产综合久久一区二区三区