如何找到 a、b、c、d、e 的组合(概率)

How to find the combinations (probability) for a,b,c,d,e

如何使用 python/algorithm 找到 a,b,c,d,e 的组合(概率)?

鉴于字符串的长度为 5,即最小 1 和最大 5。 所有字符都可以一次.

示例:

a
b
c
d
e
ab
ac
abcde
acde 
etc..
import itertools

mystring = 'abcde'
for i in range(1,len(mystring)+1):
    for combo in itertools.combinations(mystring, i):
        print(''.join(combo))

输出:

a
b
c
d
e
ab
ac
ad
ae
bc
bd
be
cd
ce
de
abc
abd
abe
acd
ace
ade
bcd
bce
bde
cde
abcd
abce
abde
acde
bcde
abcde

如果你想要排列(如评论中所述),请尝试使用 itertools.permutations:

>>> for length in range(1, 6):
...     for permutation in itertools.permutations('abcde', r=length):
...         print permutation

输出:

()
('a',)
('b',)
('c',)
('d',)
('e',)
('a', 'b')
('a', 'c')
('a', 'd')
('a', 'e')
('b', 'a')
('b', 'c')
('b', 'd')
...