python - 所有可能的排列組合問題
問題描述
暫且理解為一個(gè)字符串中字母的所有組合方法,如下,暴力而又丑陋的窮舉法。。。想請(qǐng)教下有沒有什么更好的方法,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) # 因?yàn)閮?nèi)容太多, 所以返回生成器, 可以用list方法使其變成列表print(len(set(result)))# --- 結(jié)果 ----4096回答2:
import itertoolslen(list(itertools.product(base, repeat=6)))回答3:
from itertools import productprint(list(map(''.join, product('ATCG', repeat=6))))
相關(guān)文章:
1. php怎么連接數(shù)據(jù)庫(kù)2. 在視圖里面寫php原生標(biāo)簽不是要迫不得已的情況才寫嗎3. 小皮面板MySQL怎么設(shè)置開機(jī)自起呢4. 多種方式登陸的的用戶數(shù)據(jù)表設(shè)計(jì)5. 請(qǐng)問這是jeson嗎?如何遍歷出來?6. 獲取上次登錄ip的原理是啥?7. 為什么點(diǎn)擊登陸沒反應(yīng)8. 為什么說非對(duì)象調(diào)用成員函數(shù)fetch()9. phpstudy v8打開數(shù)據(jù)庫(kù)就出錯(cuò),而phpstudy 2018不會(huì)10. 請(qǐng)問下tp6框架的緩存在哪里設(shè)置,或者說關(guān)閉?
