我想要做的是获得每种组合的所有组合和所有独特的排列。具有替换功能的组合只能让我到目前为止:
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])]
是否有更高效的算法?
答案 0 :(得分:4)
听起来你正在考虑某种“替换的排列”,其中输入'AB'
的排列大小为2会产生输出
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')]