【问题标题】:Itertools to generate scrambled combinationsItertools 生成加扰组合
【发布时间】:2013-07-28 20:02:06
【问题描述】:

我想要做的是获得所有组合以及每个组合的所有唯一排列。与替换功能的组合只让我到目前为止:

from itertools import combinations_with_replacement as cwr
foo = list(cwr('ACGT', n)) ## n is an integer

我对如何前进的直觉是做这样的事情:

import numpy as np
from itertools import permutations as perm
bar = []
for x in foo:
    carp = list(perm(x))
    for i in range(len(carp)):
        for j in range(i+1,len(carp)):
             if carp[i] == carp[j]:
                 carp[j] = ''
    carp = carp[list(np.where(np.array(carp) != '')[0])]
    for y in carp:
        bar.append(y)
for i in range(len(bar)):
    for j in range(i+1,len(bar)):
         if bar[i] == bar[j]:
             bar[j] = ''
bar = [bar[x2] for x2 in list(np.where(np.array(bar) != '')[0])]

有没有更高效的算法?

【问题讨论】:

    标签: python numpy combinatorics bioinformatics itertools


    【解决方案1】:

    听起来您正在考虑一种“替换排列”,其中排列大小为 2 的 'AB' 输入将给出输出

    AA
    AB
    BA
    BB
    

    如果是这样,那就是输入的 Cartesian product 与自身 n 次。你要itertools.product:

    >>> import itertools
    >>> list(itertools.product('AB', repeat=2))
    [('A', 'A'), ('A', 'B'), ('B', 'A'), ('B', 'B')]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多