文章詳情頁
python - 所有可能的排列組合問題
瀏覽:85日期: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. angular.js - angularjs如何傳遞id給另一個視圖 根據id獲取json數據?2. java - HashSet<int> 為何有錯誤?3. mysql - 記得以前在哪里看過一個估算時間的網站4. 使用text-shadow可以給圖片加陰影嗎?5. docker start -a dockername 老是卡住,什么情況?6. nginx啟用gzip壓縮后,文件尺寸無變化.7. 在windows下安裝docker Toolbox 啟動Docker Quickstart Terminal 失敗!8. 數據庫無法進入9. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?10. boot2docker無法啟動
排行榜
