2015-01-05 3 views
-1

파이썬/알고리즘을 사용하여 a, b, c, d, e의 조합을 찾는 방법은 무엇입니까?a, b, c, d, e의 조합 (확률)을 찾는 방법

문자열 길이가 최소 5 일 때 1 & 최대 5 모든 문자를 한 번 사용할 수 있습니다.

예 :

a 
b 
c 
d 
e 
ab 
ac 
abcde 
acde 
etc.. 
+2

[itertools] (https://docs.python.org/2/library/itertools.html) 모듈을보십시오. 그럼 당신이 아직 질문이 있으면 다시 와서 :) – fredtantini

+0

나는 "확률"이 무슨 뜻인지 보지 않는다. 가능한 모든 길이의 모든 순열을 원할 것이다. – MightyPork

+0

그래, 고마워 :) 나는 – senthilnathang

답변

4
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 
1

(주석에서 언급 한 바와 같이) 당신이 순열을 원하는 경우, 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') 
...