python - 生成范围 (i, j) 之间的核苷酸 k-mers 的所有组合

标签 python combinations permutation bioinformatics python-itertools

我需要生成一个列表,其中包含长度在 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()无论如何都不会产生像 AAAAAAATGC 这样的重复核苷酸。试试这个:

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/

相关文章:

python - 在所有 Pandas DataFrame 列中搜索字符串并过滤

algorithm - 人们可以在圆 table 上坐下多少种不同的可能方式?

c++ - 字符串与重复字符的排列(访问数组概念)

python re匹配字符串中的下划线或连字符

python - 静音 music21 警告

python - 为什么这个解决方案会失败?嵌套和匹配的括号

Matlab nchoosek 使用 int64 和 sym 得到差异答案

r - 粘贴网格——expand.grid用于字符串连接

java - 数学排列码?

c - 数组元素作为不同数组的索引