ljc*_*ljc 4 python combinations permutation bioinformatics python-itertools
我需要生成所有可能的长度在5至15之间的核苷酸组合的列表。
nucleotides = ['A', 'T', 'G', 'C']
Run Code Online (Sandbox Code Playgroud)
预期成绩:
AAAAA
AAAAT
AAAAC
AAAAG
AAATA
AAATT
...
AAAAAAAAAAAAAAA
AAAAAAAAAAAAAAT
etc.
Run Code Online (Sandbox Code Playgroud)
我试过了:
for i in range(5,16):
for j in itertools.permutations(nucleotides, i):
print j
Run Code Online (Sandbox Code Playgroud)
但是,如果这样做是行不通的len(nucleotides) < i。
提前致谢!
如果要查找所有组合,则应使用.product()as,因为它们.permutations()不会像AAAAA或那样产生重复的核苷酸AATGC。尝试这个:
for i in range(5, 16):
combinations = itertools.product(*itertools.repeat(nucleotides, i))
for j in combinations:
print(j)
Run Code Online (Sandbox Code Playgroud)
更新:如@JaredGoguen所述,该repeat参数也可以在这里使用:
combinations = itertools.product(nucleotides, repeat=i)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
564 次 |
| 最近记录: |