文章詳情頁
python - 所有可能的排列組合問題
瀏覽:168日期: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. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””2. 關于docker下的nginx壓力測試3. docker鏡像push報錯4. angular.js - angularjs的自定義過濾器如何給文字加顏色?5. docker - 如何修改運行中容器的配置6. Docker for Mac 創建的dnsmasq容器連不上/不工作的問題7. java - 如何點擊按鈕,重新運行(我是初學者)?8. 我何時應該在Java中使用JFrame.add(component)和JFrame.getContentPane()。add(component)9. html - css氣泡,實現“倒三角(不知道算不算三角了)”可透明的。10. docker 下面創建的IMAGE 他們的 ID 一樣?這個是怎么回事????
排行榜

網公網安備