我需要生成一个列表,其中包含长度在 5-15 之间的所有可能的核苷酸组合。
nucleotides = ['A', 'T', 'G', 'C']
预期结果:
AAAAA
AAAAT
AAAAC
AAAAG
AAATA
AAATT
...
AAAAAAAAAAAAAAA
AAAAAAAAAAAAAAT
etc.
我试过:
for i in range(5,16):
for j in itertools.permutations(nucleotides, i):
print j
但如果 len(nucleotides) < i
则这不起作用.
提前致谢!
最佳答案
如果你想找到所有的组合,你应该使用.product()
作为.permutations()
无论如何都不会产生像 AAAAA
或 AATGC
这样的重复核苷酸。试试这个:
for i in range(5, 16):
combinations = itertools.product(*itertools.repeat(nucleotides, i))
for j in combinations:
print(j)
更新:正如@JaredGoguen 提到的,repeat
参数也可以在这里使用:
combinations = itertools.product(nucleotides, repeat=i)
关于python - 生成范围 (i, j) 之间的核苷酸 k-mers 的所有组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36052202/