
###やりたいこと
「A」を4文字分と、「-」を17文字分使用して、合計で21文字となる文字列を作成したい。
♦︎条件
作成する文字列は、21文字分の中から、Aが入る部分、つまり4文字分を選ぶので、5,985(21C4)通りの文字列を生成することになる。
###検索して試した内容
######1,Pythonドキュメントから
https://docs.python.org/ja/3/library/itertools.html
Pythonのドキュメントから、itertoolsをimportし、以下のようにpermutationsを使用することで、作成できると考えた。
Pythonドキュメントから引用↓
itertools.permutations(iterable, r=None)
rが指定されない場合や None の場合、r はデフォルトで iterable の長さとなり、可能な最長の順列の全てが生成されます。
python3
1import itertools 2 3amino_acid = ['A','-'] 4for create_column in itertools.permutations(amino_acid, r = 21): 5 print(create_column)
引用文より、rの引数を指定してやればいいと考え、r = 21と変更してみた。
しかし、実行してみても何も実行されない。
試しに、amino_acidに入っている文字の数に指定すると、
Python3
1import itertools 2 3amino_acid = ['A','-'] 4for create_column in itertools.permutations(amino_acid, r = 2): 5 print(create_column)
以下の結果を返す。
terminal
1('A', '-') 2('-', 'A')
#疑問点
→rを設定しても意味がなかった?ようだ。しかし、ドキュメントではrの指定によって、任意の数の文字列を作成することが可能であると解釈できる文言が記述されている。これは僕の解釈ミスでしょうか?
御指南いただけると嬉しいです!よろしくお願いします!
###実は問題そのものは調べているうちに解決しました
python3
1import itertools 2 3amino_acid = ['A','-'] 4 5# a = 0 6column_list = [] 7for create_column in itertools.product(amino_acid, repeat = 21): 8 column = "" 9 # print(create_column) 10 for column_prot in create_column: 11 column += column_prot 12 count_column = column.count('A') 13 if count_column == 4: 14 column_list.append(column) 15 column = "" 16print(column_list) 17print(len(column_list))
terminal
1↓多すぎるので全て載せるのは省略 2,'-------------AA---AA-', '-------------AA---A-A', '-------------AA----AA', '-------------A-AAA---', '-------------A-AA-A--', '-------------A-AA--A-', '-------------A-AA---A', '-------------A-A-AA--', '-------------A-A-A-A-', '-------------A-A-A--A', '-------------A-A--AA-', '-------------A-A--A-A', '-------------A-A---AA', '-------------A--AAA--', '-------------A--AA-A-', '-------------A--AA--A', '-------------A--A-AA-', '-------------A--A-A-A', '-------------A--A--AA', '-------------A---AAA-', '-------------A---AA-A', '-------------A---A-AA', '-------------A----AAA', '--------------AAAA---', '--------------AAA-A--', '--------------AAA--A-', '--------------AAA---A', '--------------AA-AA--', '--------------AA-A-A-', '--------------AA-A--A', '--------------AA--AA-', '--------------AA--A-A', '--------------AA---AA', '--------------A-AAA--', '--------------A-AA-A-', '--------------A-AA--A', '--------------A-A-AA-', '--------------A-A-A-A', '--------------A-A--AA', '--------------A--AAA-', '--------------A--AA-A', '--------------A--A-AA', '--------------A---AAA', '---------------AAAA--', '---------------AAA-A-', '---------------AAA--A', '---------------AA-AA-', '---------------AA-A-A', '---------------AA--AA', '---------------A-AAA-', '---------------A-AA-A', '---------------A-A-AA', '---------------A--AAA', '----------------AAAA-', '----------------AAA-A', '----------------AA-AA', '----------------A-AAA', '-----------------AAAA'] 35985

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/08/06 12:58 編集